标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7850 | Accepted: 4818 |
Description
Input
Output
Sample Input
5 50 30 5 100 20 50 10 x x 10
Sample Output
35
1.dijkstra
/*dijkstra*/ #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <stack> #include <vector> const int inf = 0x3f3f3f; const int MAXN = 1e2+10; using namespace std; int G[MAXN][MAXN]; int vist[MAXN]; int n; char ts[10]; void init(){ memset(vist,0,sizeof(vist)); for(int i=1;i<MAXN;i++){ for(int j=1;j<=i;j++){ if(i==j) G[i][j] = 0; else G[i][j] = G[j][i] = inf; } } } int main() { while(scanf("%d",&n)!=EOF){ init(); for(int i=2;i<=n;i++){ for(int j=1;j<i;j++){ scanf("%s",ts); if(ts[0]==‘x‘){ continue; } G[i][j] = G[j][i] = atoi(ts); } } vist[1] = 1; int t,mmin; for(int i=2;i<=n;i++){ mmin = inf; for(int j=2;j<=n;j++){ if(!vist[j]){ if(mmin>G[1][j]){ t = j; mmin = G[1][j]; } } } vist[t] = 1; for(int j=2;j<=n;j++){ if(!vist[j]){ if(G[1][j]>=G[1][t]+G[t][j]){ G[1][j] = G[j][1] = G[1][t]+G[t][j]; } } } } cout<<mmin<<endl; } //cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5469231.html