poj1125 Stockbroker Grapevine

 水题,FLOYD0ms过掉,题目大意是说给你n个人可以联系的人和联系他们需要花费的时间,你求出把所有人都联系到的最少花费时间,显然要用floyd求出每一个人对其他人的最少花费,然后求出每一行中最大的值,在这些最大值中取一个最小的就可以了,记得处理i==j的情况,居然没能1Y,杯具。。。

 

#include <stdio.h>
const int size = 101;
const int eps = 1<<30-1;
int mapp[size][size];
void floyd(int n)
{
     for (int i = 1; i <= n; i ++){
         for (int j = 1; j <= n; j ++){
             for (int k = 1; k <= n; k ++){
                 if (mapp[j][k] > mapp[j][i]+mapp[i][k] && j!=k){
                    mapp[j][k] = mapp[j][i]+mapp[i][k];              
                 }   
             }   
         }   
     }    
}
int main()
{
    int n;
    while (scanf("%d", &n) != EOF && n){
          for (int i = 1; i <= n; i ++){
              for (int j = 1; j <= n; j ++){
                  mapp[i][j] = eps;
              }   
              mapp[i][i] = 0;
          }
          int IN[size]={0}, OUT[size]={0};
          for (int i = 1; i <= n; i ++){
              int t;
              scanf("%d", &t);
              while (t --){
                    int a, b;
                    scanf("%d%d", &a, &b);
                    if (mapp[i][a] > b)
                       mapp[i][a] = b;
                       IN[a] ++, OUT[b] ++;     
              }   
          }     
          bool check = false;
          for (int i = 1; i <= n; i ++){
              if (!IN[i] && !OUT[i]){
                 check = true;
              }   
          }
          if (check){
             printf("disjoint\n");
             continue;         
          }
          floyd(n);
          int ii , tt = eps;
          for (int i = 1; i <= n; i ++){
              int maxx = -1;
              int checkk = false;
              for (int j = 1; j <= n; j ++){
                  if (mapp[i][j] == eps ){checkk = true;break;}
                  if (maxx < mapp[i][j] && i!=j){
                     maxx = mapp[i][j];        
                  }
              }   
              if (tt > maxx && maxx != -1 && !checkk){
                 ii = i, tt = maxx; 
              }
          }
          printf("%d %d\n", ii, tt);
    }
    return 0;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值