zoukankan      html  css  js  c++  java
  • (floyd) hdu 4034

    Graph

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


    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
     
     
    题意
     
    给出一个方阵,a[i][j]表示i到j的最短路
    求最少需要多少边的图符合这个方阵
     
    解析:
     
    直接floyd 啊
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<string>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    int n,mp[101][101];
    bool vis[101][101],flag;
    void floyd()
    {
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(k==i||i==j||j==k)
                        continue;
                    if(mp[i][k]+mp[k][j]==mp[i][j])
                        vis[i][j]=0;
                    if(mp[i][k]+mp[k][j]<mp[i][j])
                    {
                        flag=false;
                        break;
                    }
                }
                if(!flag)
                    break;
            }
            if(!flag)
                break;
        }
    }
    int main()
    {
        int tt,cas=1;
        scanf("%d",&tt);
        while(tt--)
        {
            int ans=0;
            scanf("%d",&n);
            flag=true;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                    scanf("%d",&mp[i][j]),vis[i][j]=1;
            }
            floyd();
            if(!flag)
            {
                printf("Case %d: impossible
    ",cas++);
                continue;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(mp[i][j]==0)
                        vis[i][j]=0;
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(vis[i][j])
                        ans++;
                }
            }
            printf("Case %d: %d
    ",cas++,ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    迭代器生成器和协程函数
    装饰器
    对haproxy.conf文件的增删改查
    函数基础
    深入理解python字符编码
    python字符串列表字典常用方法
    委托的使用
    数据库连接
    输入输出流
    泛型集合的使用
  • 原文地址:https://www.cnblogs.com/water-full/p/4505338.html
Copyright © 2011-2022 走看看