zoukankan      html  css  js  c++  java
  • HDU 4034 Graph

    Graph

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 962    Accepted Submission(s): 510


    Problem 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
     
    Source
     
    Recommend
    lcy
     
     
     
     
    #include<stdio.h>
    const int MAXN=110;
    int map[MAXN][MAXN];
    int ans;//至少需要的边数
    int n;//点数 
    int Solve()
    {
        for(int i=0;i<n;i++)
          for(int j=0;j<n;j++)
             for(int k=0;k<n;k++)
             {
                 if(i!=j&&j!=k&&i!=k)
                 {
                     if(map[i][j]==map[i][k]+map[k][j])
                     {
                         ans--;//这条边可以不需要的    
                         break;
                     }    
                     if(map[i][j]>map[i][k]+map[k][j]) 
                        return 0;//不可能的情况 
                 }    
             }    
        return 1;
    }     
    int main()
    {
        int T;
        scanf("%d",&T);
    
        while(T--)
        {
            scanf("%d",&n);
            for(int i=0;i<n;i++)
              for(int j=0;j<n;j++)
                scanf("%d",&map[i][j]);
           
            ans=n*(n-1);//最多的边数
            if(Solve())
            {
                printf("%d\n",ans);
            }     
            else printf("impossible\n");
        }    
        return 0;
    }    
  • 相关阅读:
    Cheatsheet: 2018 11.01 ~ 2019 02.28
    Cheatsheet: 2018 08.01 ~ 2018 10.31
    Cheatsheet: 2018 05.01 ~ 07.31
    Cheatsheet: 2018 04.01 ~ 04.30
    stb_image multiple definition of first defined here 多文件包含问题
    NanoPi arm架构下的程序 ./ 运行黑屏 Qt环境可运行
    opencv3.4.9 + armv7 + arm-linux-gnueabihf交叉编译
    NIVIDIA Tegra K1 QWT安装使用问题和解决办法
    7.17日报
    7.16日报
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2446386.html
Copyright © 2011-2022 走看看