hdu 4034 Graph

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034

题目分类:图论

题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边

第二组样例

第三组样例:

题目分析

判断 a[i][j]==a[i][k]+a[k][j](详看代码)

代码

#include<bits/stdc++.h>

using namespace std;

int a[200][200];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif

    int t,n;
    scanf("%d",&t);
    for(int kase=1;kase<=t;kase++)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        bool ok=0;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=n;k++)
                {
                    if(i==k||k==j||i==j)
                        continue;
                    if(a[i][j]>a[i][k]+a[k][j])
                    {
                        ok=1;
                        goto here;
                    }
                    else if(a[i][j]==a[i][k]+a[k][j])
                    {
                        //printf("%d %d
",i,j);
                        ans++;
                        break;
                    }
                }
            }
        }
        here:
        printf("Case %d: ",kase);
        if(ok) printf("impossible
");
        else printf("%d
",n*n-n-ans);

    }

    return 0;
}
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
原文地址:https://www.cnblogs.com/gaoss/p/4960459.html