http://acm.timus.ru/problem.aspx?space=1&num=1160
最短路变形 水题 spfa
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<map> #include<queue> #include<stack> #include<cmath> #define LL long long //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF=0x3f3f3f3f; const int N=1005; const int M=30005; int head[N],I; struct node { int j,next,v; }side[M]; int f[N]; void Add(int i,int j,int v) { side[I].j=j; side[I].v=v; side[I].next=head[i]; head[i]=I++; } int spfa(int s,int n) { int dist[N]; bool in[N]; for(int i=1;i<=n;++i) dist[i]=INF; dist[1]=0; memset(in,false,sizeof(in)); queue<int>qt; qt.push(s); in[s]=true; while(!qt.empty()) { int x=qt.front();qt.pop(); in[x]=false; for(int t=head[x];t!=-1;t=side[t].next) { int l=side[t].j; if(dist[l]>max(dist[x],side[t].v)) { dist[l]=max(dist[x],side[t].v); f[l]=x; if(!in[l]) { in[l]=true; qt.push(l); } } } } int k=0; for(int i=1;i<=n;++i) k=max(k,dist[i]); return k; } int main() { //freopen("data.txt","r",stdin); int n,m; while(scanf("%d %d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); I=0; while(m--) { int a,b,c; scanf("%d %d %d",&a,&b,&c); Add(a,b,c); Add(b,a,c); } printf("%d\n",spfa(1,n)); printf("%d\n",n-1); for(int i=2;i<=n;++i) printf("%d %d\n",f[i],i); } return 0; }