POJ1125 Stockbroker Grapevine multi-Source Shortest Path
Theme
Given a graph, I would like to ask which vertex is used when the shortest distance from a vertex to the shortest of the shortest distance from another vertex? How long does it take?
(If someone has never been in touch, output disjoint)
Solutions
Floyd is not explained
Code
#include
#include
#include #include
using namespace std;const int INF = 1000000000;const int maxn = 110;int d[maxn][maxn];int n;int main(){ //freopen("in.txt","r",stdin); while(scanf("%d",&n) && n) { for(int i = 0 ; i < maxn ; i ++) { fill(d[i],d[i]+maxn,INF); } for(int i = 0 ; i < maxn ; i ++) d[i][i] = 0; for(int i = 1 ; i <= n ; i ++) { int t; scanf("%d",&t); while(t--) { int a,b; scanf("%d%d",&a,&b); d[i][a] = b; } } for(int k = 1 ; k <= n ; k ++) { for(int i = 1 ; i <= n ; i ++) { for(int j = 1 ; j <= n ; j ++) { d[i][j] = min(d[i][j],d[i][k]+d[k][j]); } } } int mi = INF; int mark; for(int i = 1 ; i <= n ; i ++) { int ma = -1; for(int j = 1 ; j <= n ; j ++) { if(ma < d[i][j]) ma = d[i][j]; } if(ma < mi) { mi = ma; mark = i; } } if(mi < INF) { printf("%d %d\n",mark,mi); }else printf("disjoint\n"); } return 0;}