zoukankan      html  css  js  c++  java
  • Graph (floyd)

    Description

    Everyone knows how to calculate the shortest path in a directed graph. In fact, the opposite problem is also easy. Given the length of shortest path between each pair of vertexes, can you find the original graph?
     

    Input

    The first line is the test case number T (T ≤ 100).
    First line of each case is an integer N (1 ≤ N ≤ 100), the number of vertexes.
    Following N lines each contains N integers. All these integers are less than 1000000.
    The jth integer of ith line is the shortest path from vertex i to j.
    The ith element of ith line is always 0. Other elements are all positive.
     

    Output

    For each case, you should output “Case k: ” first, where k indicates the case number and counts from one. Then one integer, the minimum possible edge number in original graph. Output “impossible” if such graph doesn't exist.
     

    Sample Input

    3
    3
    0 1 1
    1 0 1
    1 1 0
    3
    0 1 3 
    4 0 2
    7 3 0
    3
    0 1 4
    1 0 2
    4 2 0

    Sample Output
    Case 1: 6
    Case 2: 4
    Case 3: impossible 


    题目大意:给一张已经用floyd求好最短路的图,问最少可由多少条边得到。
    题目解析:对于边e[i][j],如果存在e[i][j]=e[i][k]+e[k][j],则边i->j没有必要存在;如果存在e[i][j]>e[i][k]+e[k][j],则图有误,impossible。将最外层循环放到最内层即可。

    代码如下:
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<cstring>
     4 # include<algorithm>
     5 using namespace std;
     6 int mp[105][105],n;
     7 int ok()
     8 {
     9     int i,j,k;
    10     int cnt=0,vnt=0;
    11     for(i=1;i<=n;++i){          ///枚举每
    12         for(j=1;j<=n;++j){      ///一条边
    13             if(mp[i][j]!=0)
    14                 ++vnt;
    15             if(i==j)
    16                 continue;
    17             for(k=1;k<=n;++k){  ///枚举中间节点
    18                 if(i==k||j==k)
    19                     continue;
    20                 if(mp[i][j]>mp[i][k]+mp[k][j]&&mp[i][k]&&mp[k][j]){
    21                     return -1;
    22                 }else if(mp[i][j]==mp[i][k]+mp[k][j]&&mp[i][k]&&mp[k][j]){
    23                     ++cnt;
    24                     break;
    25                 }
    26             }
    27         }
    28     }
    29     return vnt-cnt;
    30 }
    31 int main()
    32 {
    33     int T,i,j,cas=0;
    34     scanf("%d",&T);
    35     while(T--)
    36     {
    37         scanf("%d",&n);
    38         for(i=1;i<=n;++i)
    39             for(j=1;j<=n;++j)
    40                 scanf("%d",&mp[i][j]);
    41         printf("Case %d: ",++cas);
    42         int ans=ok();
    43         if(ans!=-1)
    44             printf("%d
    ",ans);
    45         else
    46             printf("impossible
    ");
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    百度缓存
    thrift文件编写
    thrift初窥
    Shutil模块介绍
    多线程访问网站的爬虫的问题
    点滴记录python linux中的个人经验
    Yii框架的form处理
    使scrapy支持ftp下载
    mojoportal学习——文章翻译之站点图标
    mojoportal学习之特色模块对Artisteer2.4生成的模板的支持
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4681835.html
Copyright © 2011-2022 走看看