POJ 1125

1Y,很难得

 

思路是选取不同的起始点,做一次Bellman Ford,从中取最大距离max_,再从所有的max_中取一个最小距离min_,最后输出min_和得到min_的那个起始点

 

#include <iostream> #include <vector> #define F(i,a,b) for (int i=a;i<=b;i++) using namespace std; int ne=0, n ,d[102]; class edge { public: int s, t, len; edge() {} edge(int a, int b, int c) { s=a, t=b, len=c; } }e[10002]; bool relax(int x) //xth edge { int s=e[x].s, t=e[x].t, len=e[x].len; if (d[t]>d[s]+len) { d[t]=d[s]+len; return true; } return false; } void bellman(int s) { fill(d, d+n+1, 0x7ffffff0); d[s]=0; bool refresh=true; for (int i=1;i<=n-1 && refresh;i++) { refresh=false; F(j,1,ne) refresh=relax(j) || refresh; } } int main() { while (cin >> n && n!=0) { int a, b, c; ne=0; F(i,1,n) { cin >> a; F(j,1,a) { cin >> b >> c;; ne++; e[ne]=edge(i, b, c) ; } } int min_=0x7ffffff0, max_, pos; F(i,1,n) { bellman(i); max_=0; F(j,1,n) if (d[j]>max_) max_=d[j]; if (max_<min_) { min_=max_; pos=i; } } if (min_==0x7ffffff0) cout << "disjoint/n"; else cout << pos << " " << min_ << endl; } return 0; }  

你可能感兴趣的:(c,BI,Class)