hdu3986Harry Potter and the Final Battle(最短路+枚举)

题意:给一个无向图,问任意删除一条边,求点1到点n的最短路径的最长长度

题解:显然删除的边只能是最短路径上的边,所以只需要记录最短路径上的边,然后枚举删除每一条,求最大值即可


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200005
#define LL long long
int cas=0,T;
int n,S,E;
const int INF = 0x3f3f3f3f  ;
struct Edge
{
	int from,to,dist;
	Edge(){}
	Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
struct HeapNode
{
	int d,u;
	bool operator < (const HeapNode &rhs)const
	{
		return d > rhs.d;
	}
};
vector<Edge>edges;
vector<int> G[maxn];
int d[maxn];
int vis[maxn];
int p[maxn];
void init()
{
    for (int i = 0;i<=n;i++)
	   G[i].clear();
   edges.clear();
}

void AddEdge(int from,int to,int dist)
{
    edges.push_back(Edge(from,to,dist));
    int mm = edges.size();
	G[from].push_back(mm-1);
}
int dijkstra(int s)               //参数s表示不走这条边
{
	priority_queue<HeapNode> q;
	for (int i = 1;i<=n;i++)
		d[i]=INF;
	d[1]=0;
	memset(vis,0,sizeof(vis));
	memset(p,0,sizeof(p));
	q.push((HeapNode){0,1});
	while (!q.empty())
	{
		HeapNode x = q.top();q.pop();
		int u = x.u;
		if (vis[u])continue;
		vis[u]=1;
		for (int i = 0;i<G[u].size();i++)
		{
			if (G[u][i]==s)
				continue;
			Edge &e = edges[G[u][i]];
			if (d[e.to] > d[u] + e.dist)
			{
                 d[e.to] = d[u] + e.dist;
				 p[e.to] = G[u][i];
				 q.push((HeapNode){d[e.to],e.to});
			}
		}
	}
	return d[n];
}
int m;
int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		init();
		for (int i = 0;i<m;i++)
		{
			int u,v,d;
			scanf("%d%d%d",&u,&v,&d);
			AddEdge(u,v,d);
			AddEdge(v,u,d);
		}
		if (dijkstra(-1) == INF)          //还没有删除边就已经走不通了
		{
		    printf("-1\n");
			continue;
		}
		vector<int >E;                    //保存路径
		int ed = n;
		while (ed!=1)
		{
			int edge = p[ed];
			E.push_back(edge);
			ed = edges[edge].from;
		}
		int ans = -1;
		for (int i = 0;i<E.size();i++)       //枚举删掉的每一条边求一次最短路
			ans = max(ans,dijkstra(E[i]));
		if (ans == INF)
			printf("-1\n");
		else
			printf("%d\n",ans);
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


题目

Description

The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case. 
 

Input

First line, case number t (t<=20). 
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities. 
 

Output

Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”. 
 

Sample Input

       
       
3 4 4 1 2 5 2 4 10 1 3 3 3 4 8 3 2 1 2 5 2 3 10 2 2 1 2 1 1 2 2
 

Sample Output

       
       
15 -1 2
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值