find the longest of the shortest HDU1595
来源:互联网 发布:有域名 如何弄企业邮箱 编辑:程序博客网 时间:2024/04/18 11:58
**题意:**Marica 要去 Mirko 所在的城市。从 Marica 所在城市到 Mirko 所在城市的所有道路中有一条路坏了不能走,但是除去这条坏的路 Marica 还是能够到达 Mirko 所在城市。Marica 总是会走最短路线,现在 Mirko 想知道最坏情况下 Marica 多长时间到达自己所在的城市。
思路:既然是最坏情况,那么坏掉的那条路就一定是最短路径中一段。因为 Marica 总是会走最短路线,所以枚举删除原最短路径中的其中一条边,再去求此时起点到终点的最短路径。这些删除原最短路径中一条边后求得的最短路径中的最大值就是答案。
这道题的题意比较难懂,懂了之后思路就不难get到了,思路对了实现对了就行了。这种难度的题目要多练练才行。
代码:
#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>#include<queue>#include<utility>#include<vector>#include<cmath>#include<set>#include<iostream>#include<algorithm>using namespace std;const int INF = 1000000;const int MAXN = 1010;int N,M;int G[MAXN][MAXN];int d[MAXN];int pro[MAXN];bool vis[MAXN];void Dijkstra(int s, bool flag) //这里的flag用来标志是否需要更新路径{ for(int i=1; i<=N; i++) d[i] = INF; memset(vis, false, sizeof(vis)); if(flag) memset(pro, 0, sizeof(pro)); d[s] = 0; if(flag) pro[s] = s; for(int i=1; i<=N; i++){ int x, m = INF; for(int j=1; j<=N; j++){ if(!vis[j] && d[j] < m){ m = d[x=j]; } } vis[x] = true; for(int j=1; j<=N; j++){ if(!vis[j] && d[j] > d[x]+G[x][j]){ d[j] = d[x] + G[x][j]; if(flag) pro[j] = x; //如果flag 为 true, 则需要记录路径 } } }}void Init(){ for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) G[i][j] = INF;}int main(){ //freopen("in.txt", "r", stdin); while(scanf("%d%d", &N, &M) == 2){ Init(); while(M--){ int a,b,c; scanf("%d%d%d", &a, &b, &c); G[a][b] = G[b][a] = c; } Dijkstra(1, true); //在没删边时跑一次得到最短路径 int t = N; int ans = -1; while(t != 1){ //遍历最短路径上的每段路,跑出这段路坏掉的情况下的最短路 int tmp = G[t][pro[t]]; G[t][pro[t]] = G[pro[t]][t] = INF; Dijkstra(1, false); ans = max(ans, d[N]); G[t][pro[t]] = G[pro[t]][t] = tmp; t = pro[t]; } printf("%d\n", ans); } return 0;}
用优先权队列实现的版本如下:
#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>#include<queue>#include<utility>#include<vector>#include<cmath>#include<set>#include<iostream>#include<algorithm>using namespace std;const int INF = 1000000;const int MAXN = 1010;struct Node{ int v,w; Node(){} Node(int a, int b):v(a), w(b){} bool operator < (const Node& n)const{ return w > n.w; }};int N,M;int G[MAXN][MAXN];int d[MAXN];int pro[MAXN];bool vis[MAXN];void Dijkstra(int s, bool flag) //这里的flag用来标志是否需要更新路径{ for(int i=1; i<=N; i++) d[i] = INF; memset(vis, false, sizeof(vis)); if(flag) memset(pro, 0, sizeof(pro)); d[s] = 0; if(flag) pro[s] = s; priority_queue<Node> que; que.push(Node(s, 0)); while(!que.empty()){ Node tmp = que.top(); que.pop(); vis[tmp.v] = true; for(int i=1; i<=N; i++){ if(!vis[i] && d[i] > d[tmp.v]+G[tmp.v][i]){ d[i] = d[tmp.v] + G[tmp.v][i]; if(flag) pro[i] = tmp.v; //如果flag 为 true, 则需要记录路径 que.push(Node(i, d[i])); } } }}void Init(){ for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) G[i][j] = INF;}int main(){ //freopen("in.txt", "r", stdin); while(scanf("%d%d", &N, &M) == 2){ Init(); while(M--){ int a,b,c; scanf("%d%d%d", &a, &b, &c); G[a][b] = G[b][a] = c; } Dijkstra(1, true); //在没删边时跑一次得到最短路径 int t = N; int ans = -1; while(t != 1){ //遍历最短路径上的每段路,跑出这段路坏掉的情况下的最短路 int tmp = G[t][pro[t]]; G[t][pro[t]] = G[pro[t]][t] = INF; Dijkstra(1, false); ans = max(ans, d[N]); G[t][pro[t]] = G[pro[t]][t] = tmp; t = pro[t]; } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- hdu1595-find the longest of the shortest
- HDU1595---find the longest of the shortest
- hdu1595 find the longest of the shortest
- HDU1595 find the longest of the shortest
- HDU1595 find the longest of the shortest
- find the longest of the shortest HDU1595
- HDU1595:find the longest of the shortest(Dijkstra)
- hdu1595 find the longest of the shortest(Dijkstra)
- HDU1595:find the longest of the shortest(Dijkstra)
- hdu1595(spfa) find the longest of the shortest
- HDU1595 find the longest of the shortest(最短路)
- HDU1595 find the longest of the shortest(最短路,Dijkstra)
- hdu1595 find the longest of the shortest (dijkstra+记录路径)
- hdu1595—find the longest of the shortest(dijkstra+枚举)
- HDU1595 find the longest of the shortest(dijk+路径记录+枚举)
- HDU1595:find the longest of the shortest(spfa+记录路径)
- find the longest of the shortest
- find the longest of the shortest HDU
- Java中HashMap的实现原理
- 如何写SysV服务管理脚本
- 网狐服务器启动的时候卡在 初始化组件
- cocos2dx 实现连接登陆服务器认证后进入游戏
- 1033. 旧键盘打字
- find the longest of the shortest HDU1595
- C语言笔记——传值调用与传址调用
- 范式理论(关系模型的核心理论)
- Html5游戏入口优势
- 图片
- 什么是序列化和反序列化 什么是串行化和并行化
- 二分查找求上界和下界
- 多级遍历
- 为什么ResNet和DenseNet可以这么深?一文详解残差块为何有助于解决梯度弥散问题