find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1916 Accepted Submission(s): 668
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.
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.
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
11 13 27
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <map>
- #include <stack>
- #include <vector>
- #include <set>
- #include <queue>
- #pragma comment (linker,"/STACK:102400000,102400000")
- #define maxn 1010
- #define MAXN 2005
- #define mod 1000000009
- #define INF 0x3f3f3f3f
- #define pi acos(-1.0)
- #define eps 1e-6
- typedef long long ll;
- using namespace std;
- struct Edge
- {
- int v,w;
- int next;
- }edge[maxn*maxn];
- int head[maxn],inq[maxn],inqtime[maxn],dist[maxn];
- int path[maxn];
- int N,num,m,a[maxn],ok;
- bool flag[maxn];
- void addedge(int u,int v,int w)
- {
- edge[num].v=v;
- edge[num].w=w;
- edge[num].next=head[u];
- head[u]=num++;
- edge[num].v=u;
- edge[num].w=w;
- edge[num].next=head[v];
- head[v]=num++;
- }
- int SPFA(int s,int n)
- {
- int i;
- memset(inq,0,sizeof(inq));
- memset(dist,INF,sizeof(dist));
- if (ok)
- {
- for (int i=0;i<=n;i++)
- path[i]=-1;
- }
- path[s]=0;
- queue<int>Q;
- inq[s]=1;
- dist[s]=0;
- Q.push(s);
- while (!Q.empty())
- {
- int u=Q.front();
- Q.pop();
- inq[u]=0;
- for (i=head[u];i;i=edge[i].next)
- {
- if (dist[ edge[i].v ]>dist[u]+edge[i].w)
- {
- if (ok)
- path[ edge[i].v ]=u;
- dist[ edge[i].v ]=dist[u]+edge[i].w;
- if (!inq[ edge[i].v ])
- {
- inq[ edge[i].v ]=1;
- Q.push(edge[i].v);
- }
- }
- }
- }
- return dist[N];
- }
- int main()
- {
- int i,j;
- while (~scanf("%d%d",&N,&m))
- {
- int u,v,w;
- memset(head,0,sizeof(head));
- num=1;
- ok=1;
- for (i=0;i<m;i++)
- {
- scanf("%d%d%d",&u,&v,&w);
- addedge(u,v,w);
- }
- // printf("num=%d\n",num);
- SPFA(1,N);
- ok=0;
- j=N;
- int ans=-1;
- while (path[j]>0)
- {
- int x,y,value;
- for (int i=head[j];i!=0;i=edge[i].next)
- {
- if (edge[i].v==path[j])
- {
- x=i;
- value=edge[i].w;
- edge[i].w=INF;
- break;
- }
- }
- for (int i=head[path[j]];i!=0;i=edge[i].next)
- {
- if (edge[i].v==j)
- {
- y=i;
- edge[i].w=INF;
- break;
- }
- }
- ans=max(ans,SPFA(1,N));
- edge[x].w=value;
- edge[y].w=value;
- // printf("j=%d\n",j);
- j=path[j];
- }
- // printf("\n%d\n",path[1]);
- printf("%d\n",ans);
- }
- return 0;
- }
find the longest of the shortest (hdu 1595 SPFA+枚举)的更多相关文章
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu1595 find the longest of the shortest(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...
- hdu 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 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 ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...
- HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
随机推荐
- 6,id 小数据池,编码and解码。
asscii :8位(数字,英文,特殊字符)表示一个字符. A:0000 0001 unicode :(万国码)初期:16位,两个字节,表示两个字符. A:0000 0001 0000 0001 中: ...
- 45个有用的JavaScript技巧
众所周知,JavaScript是世界上最流行的变成语言,不管是web网页,手机APP(例如PhoneGap或Appcelerator),还是服务器端(例如NodeJS或Wakanda)还有许多其他的实 ...
- debian 9 安装AMD驱动
目录 debian 9 安装AMD驱动 安装驱动之前: 安装驱动: 安装驱动之后: debian 9 安装AMD驱动 需求说明: 安装完成debian系统后独显驱动未安装 操作系统版本: kyeup@ ...
- 关于面试总结-python笔试题
关于面试总结4-python笔试题 前言 现在面试测试岗位,一般会要求熟悉一门语言(python/java),为了考验求职者的基本功,一般会出2个笔试题,这些题目一般不难,主要考察基本功. 要是给你一 ...
- xtu数据结构 C. Ultra-QuickSort
C. Ultra-QuickSort Time Limit: 7000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java ...
- numpy hstack()
numpy.hstack(tup)[source] Stack arrays in sequence horizontally (column wise). Take a sequence of ar ...
- [转载] Laya性能优化精选内容整理
第一是性能统计工具,这是LayaAir引擎内置的性能统计工具,在代码加入Laya.Stat.show(); 引擎内置的性能统计工具 打开这个工具后,可以用于观察性能,除了FPS越高越好外,其它的值越低 ...
- ubuntu添加开机启动
vim /etc/init.d/mytest #!/bin/sh echo "$(pwd) and $USER and $(whoami)" >> /root/temp ...
- 在VS2017中编写Python程序
最近开始了python的学习,在搭建完python环境之后,在选择IDE的时候陷入了困境,首先选择的是PyCharm但是用着还是不习惯,毕竟用VS开发了几年了,突然换软件总感觉有点不适应,就想到了强大 ...
- DDLog-不同颜色打印信息
(一)下载安装 1.安装插件 XcodeColors Github 链接:https://github.com/robbiehanson/XcodeColors 打开XcodeColors项目,编译即 ...