标签:pre distance tput sep sample limit lin code first
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 81024 | Accepted: 26725 |
Description
Input
Output
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; const int maxn = 1010; const int INF = 0x3f3f3f3f; int mp[maxn][maxn],dis[maxn],vis[maxn]; int edge,node; void dijkstra(){ for(int i = 1; i <= node; i++){ dis[i] = mp[1][i]; } for(int i = 1; i < node; i++){ int minn = INF,u; for(int j = 1; j <= node; j++){ if(vis[j] == 0 && dis[j] < minn){ minn = dis[j]; u = j; } } vis[u] = 1; for(int j = 1; j <= node; j++){ if(vis[j] == 0 && dis[u] + mp[u][j] < dis[j]){ dis[j] = mp[u][j] + dis[u]; } } } printf("%d\n",dis[node]); } int main(){ while(~scanf("%d%d",&edge,&node)){ for(int i = 1; i <= node; i++){ for(int j = 1; j <= node; j++){ if(i==j) mp[i][j] = 0; else mp[i][j] = INF; } } memset(vis,0,sizeof(vis)); int m,n,t; for(int i = 0; i < edge; i++){ scanf("%d%d%d",&n,&m,&t); if(t < mp[m][n]){ mp[m][n] = mp[n][m] = t; } } dijkstra(); } return 0; }
(Dijkstra) POJ2387 Til the Cows Come Home
标签:pre distance tput sep sample limit lin code first
原文地址:https://www.cnblogs.com/Weixu-Liu/p/10417903.html