标签:
#include <iostream> #include <cstdio> #include <cstring> #define INF 0x7ffffff using 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; }
标签:
原文地址:http://www.cnblogs.com/immortal-worm/p/4936984.html