HDU 1595 find the longest of the shortest(删边+多次dijkstra)

传送门

Problem Description
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn’t live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn’t konw exactly which road. It is possible to come from Marica’s city to Mirko’s no matter which road is closed.
Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.

Input
Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.

Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.

Sample Input

5 6
1 2 4
1 3 3
2 3 1
2 4 4
2 5 7
4 5 1

6 7
1 2 1
2 3 4
3 4 4
4 6 4
1 5 5
2 5 2
5 6 5

5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10

Sample Output

11
13
27

题意: 题意很绕,费了我些时间理解。大致就是给出n个城市(从1到n编号)和m条路。要你计算从1到n的最短路。但是!所有路中,可能存在某条路被封锁,要求你算出最糟糕的情况下(只封锁一条路)的最短路。
题解: 懒一点的,来一个循环,删除某条边,计算最短路,然后恢复这条边;继续删除下一条边……这么多边,想想也应该会超时。所以应当先计算一次最短路,把没有封路情况下的最短路记录下来,然后我依次去删除最短路径中的某条边,计算最短路,恢复;继续重复……
折腾我的是,在我原来的dijkstra模板中,我是定义了一个结构体来村边,不定数组的下标表示某条边的起点,却没有东西表示终点,导致我的删边和恢复操作很难执行。但如果像Floyd那样定义一个二维数组,操作起Dijkstra又非常的别扭。所以我去找到我的SPFA的链式前向星的模板,把其中的存图方式搬了过来。

void addedge(int x,int y,int w){
	e[cnt].to = y;
	e[cnt].w = w;
	e[cnt].next = head[x];
	head[x] = cnt++;
}

当初觉得这个东西很烦,直接套Dijkstra的模板不好嘛;现在——真香啊。在这里插入图片描述

有过这个之后,我们就能很方便地进行删边和恢复。通过记录前驱节点数组pre[]来记录最短路径上的点,同时辅以另一个数组来记录最短路径上的边pre[t.to] = p.id; //记录前驱节点 id[t.to] = i; //记录到达这个点的边的序号有过这些之后就能操作了,具体见代码。

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxm = 1e6;
const int maxn = 1006;

struct edge{
	int to, w, next;
} e[maxm];

struct node{
	int id, s;
	node(int a,int b){id = a;s = b;}
	bool operator <(const node a)const{
		return a.s < s;}
};

int n, m, cnt;
int id[maxm];					//最短路上经过的边的序号
int vis[maxn];					//标记数组
int head[maxm];					//链式前向星头结点
int dis[maxn];					//距离数组
int pre[maxn];					//前驱数组
priority_queue<node>q;

void addedge(int x,int y,int w){
	e[cnt].to = y;
	e[cnt].w = w;
	e[cnt].next = head[x];
	head[x] = cnt++;
}

void Clear(){
	for (int i = 1; i <= n; i++){
		vis[i] = 0;
		dis[i] = inf;
	}
	while(!q.empty())
		q.pop();
}

void init(){
	cnt = 0;
	for (int i = 1; i <= n; i++){
		head[i] = pre[i] = -1;
		id[i] = 0;
	}
}

int dijkstra(int flag,int n){
	Clear();
	dis[1] = 0;
	q.push(node(1, 0));
	while(!q.empty()){
		node p = q.top();
		q.pop();
		if(vis[p.id])
			continue;
		vis[p.id] = 1;
		for(int i = head[p.id]; ~i; i = e[i].next){	//head[p.id]访问的是边的编号 
			edge t = e[i];
			if(!vis[t.to] && dis[t.to] > dis[p.id] + t.w){//松弛 
				dis[t.to] = dis[p.id] + t.w;
				q.push(node(t.to,dis[t.to]));
				if(flag == 1){					
					pre[t.to] = p.id;				//记录前驱节点 
					id[t.to] = i;					//记录到达这个点的边的序号 
				}
			}
		}
	}
	if(dis[n] == inf)
		return -1;
	return dis[n];
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		init();
		for(int i = 0; i < m; i++){
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			addedge(x, y, z);
			addedge(y, x, z);
		}
		int ans = dijkstra(1,n);
		int t = n;
		while(t != -1){					//枚举最短路经过的所有边,分别进行一次"删除"
			int w = e[id[t]].w;
			e[id[t]].w = inf;
			ans = max(ans, dijkstra(0, n));
			e[id[t]].w = w;	
			t = pre[t];
		}	
		printf("%d\n",ans);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值