#HDU 3986 Harry Potter and the Final Battle (枚举 + 最短路 + 保存点和边操作)


 

Problem 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

题目大意 :输入一张无向图, 输出删去一条边后的最短路(取最坏的情况), 每个点之间可能有多条边

思路 : 这道题是HDU 1595 的升级版, 1595里的没有重边,如果直接每次删一条边然后求最短路的话肯定会超时, 所以我们先跑一遍最短路, 将最短路径给记下来, 再删掉该路径上的每条边即可。 如果碰到重边的话单纯这样写就不行了,比如点 1 到点 2,有三条边, 权值分别为 5, 10, 15. 那么答案应该是10, 取得是次大值。所以在第一次跑的时候,我们把点和边都保存下来, 每次将该边权值记为INF, 跑完再改回去就OK了。 不用担心最短路径如果有多条,因为如果有多条,怎么删都不会该边最短路

Accepted code

#include<bits/stdc++.h>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const int MAXM = 1e3 + 10;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	int v, w, next;
}e[MAXN];
struct node
{
	int id, w;
	bool operator < (const node &oth) const
	{
		return w > oth.w;
	}
}mid;
int head[MAXN], n, m, cnt, T;
int pre[MAXN], dis[MAXN], edge[MAXN];
void init() {
	MEM(e, 0); MEM(head, -1);
	MEM(pre, -1); MEM(edge, 0);
	cnt = 0;
}
void add(int from, int to, int wi) {
	e[++cnt].v = to;
	e[cnt].w = wi;
	e[cnt].next = head[from];
	head[from] = cnt;
}
void dijkstra(int u, int flag) {
	priority_queue <node> q;
	MEM(dis, INF); dis[u] = 0;
	if (flag) MEM(pre, -1);
	q.push({ u, 0 });
	while (!q.empty()) {
		mid = q.top();
		q.pop();
		int ans = mid.id;
		if (dis[ans] != mid.w) continue;
		for (int i = head[ans]; i != -1; i = e[i].next) {
			int vi = e[i].v;
			if (dis[vi] > dis[ans] + e[i].w) {
				dis[vi] = dis[ans] + e[i].w;
				q.push({ vi, dis[vi] });
				if (flag) {
					pre[vi] = ans; // 存点
					edge[vi] = i;  // 存边
				}
			}
		}
	}
}

int main()
{
	cin >> T;
	while (T--) {
		sc("%d %d", &n, &m);
		init();
		for (int i = 0; i < m; i++) {
			int ui, vi, wi;
			sc("%d %d %d", &ui, &vi, &wi);
			add(ui, vi, wi); add(vi, ui, wi);
		}
		dijkstra(1, 1);
		if (dis[n] == INF) { cout << -1 << endl; continue; }
		int ans = -1;
		for (int i = n; i != -1; i = pre[i]) {
			int tmp = edge[i], wi = e[tmp].w;
			e[tmp].w = INF;  // 删除该边
			dijkstra(1, 0);
			if (dis[n] == INF) continue;
			ans = max(ans, dis[n]);
			e[tmp].w = wi; // 改回来
		} 
		cout << ans << endl;
	}
	return 0;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值