/** \brief poj 1502--Dijkstra * * \ date 2014/7/15 * \ state AC * * */ #include <iostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; #define inf 0x03f3f3f3f const int MAXN=101; int n; int cost[MAXN][MAXN]; int path[MAXN]; int vis[MAXN]; int lowcost[MAXN]; void input() { char st[100]; for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) { scanf("%s", st); if (st[0] == 'x') cost[i][j] = cost[j][i] = inf; else cost[i][j] = cost[j][i] = atoi(st); } } void Dijkstra(int cost[][MAXN],int lowcost[MAXN],int n,int beg) { int i,j,Min; memset(vis,0,sizeof(vis)); vis[beg]=1; for(i=0;i<n;i++) { lowcost[i]=cost[beg][i]; path[i]=beg; } lowcost[beg]=0; path[beg]=-1;//树根的标记 int pre=beg; for(i=1;i<n;i++) { Min=inf; for(j=0;j<n;j++) //下面的加法可能导致溢出,inf不能取太大 if(vis[j]==0 && lowcost[pre]+cost[pre][j]<lowcost[j]) { lowcost[j]=lowcost[pre]+cost[pre][j]; path[j]=pre; } for(j=0;j<n;j++) if(vis[j]==0 && lowcost[j]<Min) { Min=lowcost[j]; pre=j; } vis[pre]=1; } } int main() { //cout << "Hello world!" << endl; //freopen("input.txt","r",stdin); cin>>n; input(); Dijkstra(cost,lowcost,n,0); int ans = 0; for (int i = 0; i < n; i++) ans = max(ans, lowcost[i]); cout<<ans<<endl; return 0; }
poj1502--Dijkstra,布布扣,bubuko.com
原文地址:http://blog.csdn.net/greenapple_shan/article/details/37886009