这道题又是Dijkstra,但是layer比较多要小心。每一个存储都要在合适的时候清空。
图像的题最好还是自己画在纸上好好研究之后再动手写。
#include <iostream>
#include <vector>
using namespace std;
int INF=20000;
int no_stockholders=0;
int weight[200][200];
int t[200];
int max_t[200];
vector<int> Q;
int Extract_Min(){
int record=INF+1;
int index=1;
for(int i=0;i<Q.size();i++){
if(record>t[Q[i]]){
record=t[Q[i]];
index=Q[i];
}
}
return index;
}
int main(int argc, const char * argv[]) {
// insert code here...
while(cin>>no_stockholders){
if(no_stockholders==0)
break;
for(int i=1;i<=no_stockholders;i++){
int no_contacts;
cin>>no_contacts;
for(int j=1;j<=no_stockholders;j++){
weight[i][j]=-1;
}
for(int j=0;j<no_contacts;j++){
int v,ti;
cin>>v>>ti;
weight[i][v]=ti;
}
}
for(int i=0;i<no_stockholders;i++){
max_t[i+1]=INF;
}
for(int i=1;i<=no_stockholders;i++){
for(int j=1;j<=no_stockholders;j++){
t[j]=INF;
}
t[i]=0;
for(int j=0;j<no_stockholders;j++){
Q.push_back(j+1);
}
while(!Q.empty()){
int u=Extract_Min();
vector<int>::iterator it;
it=Q.begin();
for(int j=0;j<Q.size();j++,it++){
if(Q[j]==u)
Q.erase(it);
}
if(t[u]==INF)
break;
for(int v=1;v<=no_stockholders;v++){
if(weight[u][v]!=-1){
int w=weight[u][v];
if(t[u]+w<t[v])
t[v]=t[u]+w;
}
}
}
int record=t[1];
for(int j=1;j<=no_stockholders;j++){
if(t[j]==INF){
record=INF+1;
break;
}
if(t[j]>record)
record=t[j];
}
max_t[i]=record;
}
int r=max_t[1];
int in=1;
for(int j=1;j<=no_stockholders;j++){
if(max_t[j]<r){
r=max_t[j];
in=j;
}
}
cout<<in<<" ";
if(max_t[in]==INF+1)
cout<<"disjoint";
else
cout<<max_t[in];
cout<<endl;
}
return 0;
}