HDU-3986 最短路(SPFA+记录路径)

题目链接:>_>

题目:Harry Potter and the Final Battle

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

题目大意:

给一个无向图,问任意删除一条边,求点 1 到点 n 的最短路径的最长长度,如果能使1不能到达n,输出-1。

思路:

链式前向星存图,spfa+记录路径,找到最短路,然后枚举删去最短路中的边,并跑一遍spfa,找到最大的最短路。

代码:

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 10005;
const int maxm = 500005;
int n, m, a, b, cnt = 0;
int dis[maxn], vis[maxn], head[maxm];
int pre[maxn], pre2[maxn];
struct node {
	int next, to, w;
}x[maxm];
void add(int start, int endd, int w) {
	x[cnt].to = endd;
	x[cnt].w = w;
	x[cnt].next = head[start];
	head[start] = cnt++;
}
void init() {
	cnt = 0;
	memset(head, -1, sizeof head);
	memset(x, 0, sizeof x);
	memset(pre, 0, sizeof pre);
	memset(pre2, 0, sizeof pre2);
}
int spfa() {
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		dis[i] = inf; //初始化
		vis[i] = 0;
	}
	q.push(a);
	dis[a] = 0; 
	vis[a] = 1; //第一个顶点入队,进行标记
	while (!q.empty()) {
		int u = q.front(); //取出队首
		q.pop(); 
		vis[u] = 0; //出队标记
		for (int i = head[u]; i != -1; i = x[i].next) {//找这条边的同起点的上一条边
			int v = x[i].to;
			if (dis[v] > dis[u] + x[i].w) {//如果有最短路就更改
				dis[v] = dis[u] + x[i].w;
				if (vis[v] == 0) {//未入队则入队
					vis[v] = 1; //标记入队
					q.push(v);
				}
				pre[v] = u;
				pre2[v] = i;
			}
		}
	}
	if (dis[b] >= inf)return 0;
	return dis[b];
}
int spfa2() {
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		dis[i] = inf; //初始化
		vis[i] = 0;
	}
	q.push(a);
	dis[a] = 0;
	vis[a] = 1; //第一个顶点入队,进行标记
	while (!q.empty()) {
		int u = q.front(); //取出队首
		q.pop();
		vis[u] = 0; //出队标记
		for (int i = head[u]; i != -1; i = x[i].next) {//找这条边的同起点的上一条边
			int v = x[i].to;
			if (dis[v] > dis[u] + x[i].w) {//如果有最短路就更改
				dis[v] = dis[u] + x[i].w;
				if (vis[v] == 0) {//未入队则入队
					vis[v] = 1; //标记入队
					q.push(v);
				}
			}
		}
	}
	if (dis[b] >= inf)return 0;
	return dis[b];
}
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		init();
		cin >> n >> m;
		a = 1, b = n;
		for (int i = 1; i <= m; i++) {
			int q1, q2, q3;
			cin >> q1 >> q2 >> q3;
			add(q1, q2, q3); //有向图
			add(q2, q1, q3); //无向边中结构体数组要开到边数的两倍
		}
		int c = spfa();
		if (!c) {
			cout << "-1" << endl;
			continue;
		}
		int te = b;
		int maxnn = -1;
		while (te != 1) {
			int e = pre2[te];
			int w2 = x[e].w;
			x[e].w = inf;
			int u = spfa2();
			if (!u) {
				maxnn = -1;
				break;
			}
			else {
				maxnn = max(maxnn, u);
			}
			x[e].w = w2;
			te = pre[te];
		}	
		printf("%d\n", maxnn);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值