hdu 1595 find the longest of the shortest(dijkstra)
2015-09-23 18:55
483 查看
[align=left]Problem Description[/align]
[align=left]Input[/align]
[align=left]Output[/align]
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
[align=left]Source[/align]
HDU 2007-Spring Programming Contest - Warm Up (1)
题意:城市内有n条路,其中有某条路在修,因为这条路有很多情况,问各种情况下的最短路中最长的是哪条
思路:直接枚举最短路的所有边,将边标记为inf,再跑dijkstra算出最大值即可
View Code
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
11 13 27
[align=left]Source[/align]
HDU 2007-Spring Programming Contest - Warm Up (1)
题意:城市内有n条路,其中有某条路在修,因为这条路有很多情况,问各种情况下的最短路中最长的是哪条
思路:直接枚举最短路的所有边,将边标记为inf,再跑dijkstra算出最大值即可
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<bitset> #include<map> #include<vector> #include<stdlib.h> #include <stack> using namespace std; int dirx[]={0,0,-1,1}; int diry[]={-1,1,0,0}; #define PI acos(-1.0) #define max(a,b) (a) > (b) ? (a) : (b) #define min(a,b) (a) < (b) ? (a) : (b) #define ll long long #define eps 1e-10 #define MOD 1000000007 #define N 1006 #define inf 1<<26 int n,m; int mp ; int fa ; void init(){ for(int i=1;i<=n;i++){ fa[i]=-1; for(int j=1;j<=n;j++){ if(i==j){ mp[i][j]=0; } else{ mp[i][j]=inf; } } } } int dijkstra(int flag){ int vis ; int dis ; for(int i=0;i<=n;i++){ vis[i]=0; dis[i]=inf; } vis[1]=1; dis[1]=0; int x=n; int st=1; while(x--){ for(int i=1;i<=n;i++){ if(dis[i]>dis[st]+mp[st][i]){ dis[i]=dis[st]+mp[st][i]; if(flag){ fa[i]=st; } } } int minn=inf; for(int i=1;i<=n;i++){ if(!vis[i] && dis[i]<minn){ minn=dis[i]; st=i; } } vis[st]=1; } return dis ; } int main() { while(scanf("%d%d",&n,&m)==2){ init(); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(mp[a][b]>c){ mp[a][b]=mp[b][a]=c; } } int ans=dijkstra(1); for(int i=n;i!=1;i=fa[i]){ int num=mp[i][fa[i]]; mp[i][fa[i]]=mp[fa[i]][i]=inf; int cnt=dijkstra(0); ans=max(ans,cnt); mp[i][fa[i]]=mp[fa[i]][i]=num; } printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- Linux在虚拟机中无法上网的解决方法
- Python 学习(5)---类的相关知识整理(续)
- 用ReactJS写的简单组件Pagebar(待完善。。。)
- hdoj 3911 Black And White 【线段树区间异或 + 合并】
- [算法导论 第2章]归并排序
- GPS定位\基站定位\WiFi辅助定位及其其他定位原理详解
- 【CSS3】纯CSS代码实现模拟时钟,+js对时功能。
- CentOS 6.6下的J2EE环境搭建(三)之Apache安装
- Mybatis类型转换TypeHandler介绍
- MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突
- 第四周【项目3.2-单链表应用(多文件)把一个链表接到另一个后面】
- 杭电acm2524
- mysql将a表字段插入到b表
- poj2079(一堆点找出最大的三角形)
- 升级Xcode7后遇到的一些问题
- WebSphere常用设置
- eclipse中jdk的配置
- LeetCode(50) Binary Tree Inorder Traversal 中序遍历
- 关于swift与objective-c混合编译
- 关于ajax图片上传,上传是后台代码来完成的,利用ajaxSubmit来完成,前台只是负责提交数据