Stockbroker Grapevine http://poj.org/problem?id=1125

模板题

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int inf=0x3f3f3ff;
 5 const int M=128;
 6 class Floyd{///多源最短路o(MV^3)
 7     typedef int typec;///边权的类型
 8     static const int MV=128;///点的个数
 9     int n,i,j,k;
10     typec mat[MV][MV];
11 public:
12     void init(int tn){///传入点的个数,下标0开始
13         n=tn;
14         for(i=0;i<n;i++)
15             for(j=0;j<n;j++)
16                 mat[i][j]=i==j?0:inf;
17     }
18     void add(int u,int v,typec w){
19         mat[u][v]=min(mat[u][v],w);
20     }
21     void solve(){
22         for(k=0; k<n; k++)
23             for(i=0; i<n; i++)
24                 for(j=0; j<n; j++)
25                     mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);
26     }
27     typec getdist(int x,int y) {
28         return mat[x][y];
29     }
30 }gx;
31 int main() {
32     int n,m,x,y;
33     while(~scanf("%d",&n),n) {
34         gx.init(n);
35         for(int i=0; i<n; i++) {
36             scanf("%d",&m);
37             while(m--) {
38                 scanf("%d%d",&x,&y);
39                 gx.add(i,x-1,y);
40             }
41         }
42         gx.solve();
43         int sma=inf;
44         int id;
45         for(int i=0; i<n; i++) {
46             int sum=0;
47             for(int j=0; j<n; j++) {
48                 sum+=gx.getdist(i,j);
49             }
50             if(sma>sum) {
51                 sma=sum;
52                 id=i;
53             }
54         }
55         int big=0;
56         for(int i=0; i<n; i++) {
57             big=max(big,gx.getdist(id,i));
58         }
59         printf("%d %d\n",id+1,big);
60     }
61     return 0;
62 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
  • 2021-06-07
  • 2021-06-12
  • 2021-05-31
  • 2022-01-26
猜你喜欢
  • 2021-10-16
  • 2021-09-26
  • 2022-02-03
  • 2022-01-25
  • 2021-11-04
  • 2021-07-26
相关资源
相似解决方案