标签:hdu 2544 最短路 dijkstra flyod
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include<iostream> #include<cstring> #include<cstdio> #define INF 0xfffffff //十六进制 #define N 105 using namespace std; int map[N][N],vis[N],dis[N]; int n; void Dijkstra() { int i,j; for(i=1; i<=n; i++) { dis[i]=map[1][i];///给出出发点到各个地点的距离 vis[i]=false;///路径指示为还未走 //cout<<dis[i]<<' '; } for(i=1; i<=n; i++) { int tmp=INF; int k; ///假设一变量表示两地之间所花时间 for(j=1; j<=n; j++) { if(!vis[j]&&dis[j]<tmp) ///找出花时间最短的一条路径k tmp=dis[k=j]; } vis[k]=true; for(j=1; j<=n; j++) { if(!vis[j]&&dis[j]>dis[k]+map[k][j]) ///判断从出发点到j的时间 是否大于 经过k而到j的距离 dis[j]=dis[k]+map[k][j]; } } cout<<dis[n]<<endl; } int main() { int i,j,m; while(cin>>n>>m&&(n||m)) { for(i=0; i<N; i++) for(j=0; j<N; j++) map[i][j]=INF; int a,b,time; for(i=1; i<=m; i++) { cin>>a>>b>>time; if(time<map[a][b]) map[a][b]=map[b][a]=time; ///判断并给出两地间最短距离 } Dijkstra(); } return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 100010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 typedef long long ll; const int INF=1000010; using namespace std; int d[110][110]; int main() { int i,j,k,n,m,p,q,s; while(scanf("%d%d",&n,&m)!=EOF,n+m) { for (i=1; i<=n; i++) { for(j=1; j<=n; j++) { d[i][j]=INF; } } for (i=0; i<m; i++) { scanf("%d%d%d",&p,&q,&s); d[p][q]=d[q][p]=s; } for (k=1; k<=n; k++) { for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { if (d[i][j]>d[i][k]+d[k][j]) { d[i][j]=d[i][k]+d[k][j]; } } } } printf("%d\n",d[1][n]); } return 0; }
标签:hdu 2544 最短路 dijkstra flyod
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/41451903