HDU-3665(单源最短路)
来源:互联网 发布:三毛淘宝小号批发 编辑:程序博客网 时间:2024/06/07 05:52
Problem Description
XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.
Input
There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that the distance between the i-th town and the SMi town is LMi.
Output
Each case takes one line, print the shortest length that XiaoY reach seaside.
Sample Input
5 1 0 1 1 2 0 2 3 3 1 1 1 4 100 0 1 0 1
Sample Output
2
思路:
在Dijkstra的基础上稍作修改即可,注意0x7fffffff可能会超范围
#include <iostream>#include <cstdio>#include <cstring>#define INF 0x7ffffffusing namespace std;int N;int G[17][17];int tmp;int sea[17];int a,b;int s[17];int d[17];int minn; int v;int ans;int st[107];int min(int a,int b){ return a<b?a:b;}int main(){ while(~scanf("%d",&N)) { if(N == 0){ printf("0\n"); continue; } ans = INF; memset(s,0,sizeof(s)); for(int i = 0;i < N;i++) for(int j = 0;j < N;j++) G[i][j] = i==j?0:INF; for(int i = 0;i < N;i++) { scanf("%d%d",&tmp,&sea[i]); while(tmp--) { scanf("%d%d",&a,&b); G[i][a] = b; } } if(sea[0]){ printf("0\n"); continue; } s[0] = 1; for(int i = 0;i < N;i++) d[i] = G[0][i]; for(int i = 1;i < N;i++) { minn = INF; for(int j = 0;j < N;j++) if(!s[j] && d[j]<minn) minn = d[v=j]; s[v] = 1; if(sea[v]) ans = min(ans,minn); for(int j = 0;j < N;j++) if(!s[j] && d[j]>G[v][j]+minn) d[j] = G[v][j]+minn; } printf("%d\n",ans); } return 0;}
0 0
- HDU-3665(单源最短路)
- HDU 2544 最短路 (单源最短路)
- hdu 2112 HDU Today (单源最短路)
- HDU 3665 Seaside (最短路--floyd)
- hdu 3665(最短路)
- hdu 3790 (单源最短路dijkstra)
- HDU 1142(单源最短路,记忆DFS)
- HDU-2544单源最短路
- 单源最短路算法HDU 2544 ( 最短路 )
- HDU 2544 - 最短路(单源最短路)
- HDU HDU - 3416 (单源最短路 + 最大流)
- HDU 3665 Seaside 最短路
- hdu 3665 Seaside(最短路)
- hdu 3665 Seaside 最短路
- HDU2112 HDU Today 单源最短路
- hdu 3191+hdu 1688(最短路+次短路)
- hdu 1874 (最短路)
- hdu 3499 (最短路)
- HDU-2059龟兔赛跑(基础方程DP-遍历之前的所有状态)
- 多线程学习
- ls
- HDU-3661(贪心)
- 欢迎使用CSDN-markdown编辑器
- HDU-3665(单源最短路)
- HDU-4857(拓扑排序)
- ORA-39087,ORA-39070,ORA-39002
- lintcode ----二分查找
- mysql 常见命令
- HDU-1176(基础方程DP)
- Akka/play(activator) 2.5.3 创建工程 2
- HDU-5504(逻辑if-else大水题)
- HDU-5532(LIS-nlogn)