POJ 1125-Stockbroker Grapevine-最短路
裸最短路
/*--------------------------------------------------------------------------------------*/ // Helica's header // Second Edition // 2015.11.7 // #include <algorithm> #include <iostream> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map> //debug function for a N*M array #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\ {for(int j=0;j<(M);j++){\ printf("%d",G[i][j]);}printf("\n");} //debug function for int,float,double,etc. #define debug_var(X) cout<<#X"="<<X<<endl; /*--------------------------------------------------------------------------------------*/ using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 300; int N,M,T; struct qnode { int v,c; qnode(int _v=0,int _c=0):v(_v),c(_c){} bool operator < (const qnode &r) const {return c > r.c;} }; struct Edge{ int v,cost; Edge(int _v=0,int _cost=0):v(_v),cost(_cost){} }; vector <Edge> E[MAXN]; bool vis[MAXN]; int dist[MAXN]; int Dijkstra(int n,int start) { memset(vis,false,sizeof vis); for(int i=1;i<=n;i++) dist[i] = INF; priority_queue<qnode> que; while(!que.empty()) que.pop(); dist[start] = 0; que.push(qnode(start,0)); qnode tmp; while(!que.empty()) { tmp = que.top(); que.pop(); int u = tmp.v; if(vis[u]) continue; vis[u] = true; for(int i=0;i<(int)E[u].size();i++) { int v = E[u][i].v; int cost = E[u][i].cost; if(!vis[v]&&dist[v]>dist[u]+cost) { dist[v] = dist[u]+cost; que.push(qnode(v,dist[v])); } } } int ans = 0; for(int i=1;i<=n;i++) ans = max(ans,dist[i]); //printf("start:%d max:%d\n",start,ans); return ans; } void addedge(int u,int v,int w) { E[u].push_back(Edge(v,w)); } int main() { while(scanf("%d",&N) && N) { for(int i=0;i<MAXN;i++) E[i].clear(); for(int i=1,m,id,t;i<=N;i++) { scanf("%d",&m); for(int j=0;j<m;j++) { scanf("%d%d",&id,&t); addedge(i,id,t); } } int res = INF,resid = 0; for(int i=1,tmp;i<=N;i++) { tmp = Dijkstra(N,i); if(res > tmp) { res = tmp; resid = i; } } printf("%d %d\n",resid,res); } }