HDU 4034 Graph

                                                                         Graph

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


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
题目大意:
给出一个矩阵,代表已经经过处理的数据,数据代表从i到j的最短路程,现在让求出原先有多少条路存在。
解题思路:
弗洛伊德的应用,仔细想想不难想到,当从i到j的路程等于从i到k的路程加从k到j的路程,从i到j的路可以删掉,
当从i到j的 路程大于从i到k的路程加从k到j的路程,则就说明该种情况不存在。应该注意的是,当删除掉路时间,
应该标记,数据中应该有数据使删除的路次数重复,因此应该标记,我就是被卡在这地方的。
代码:
#include<stdio.h> #include<string.h> int main() {     int a[110][110];     int b[110][110];     int i,j,k,t,n,x,y,v;     scanf("%d",&t);     v=1;     while(t--)     {         y=0;         scanf("%d",&n);         for(i=1;i<=n;i++)            for(j=1;j<=n;j++)              {              scanf("%d",&a[i][j]);              if(i==j)                 a[i][j]=-1;              }              memset(b,0,sizeof(b));          int z=0;         for(i=n;i>0;i--)          for(j=n;j>0;j--)          {            for(k=n;k>0;k--)            {              if(a[i][k]!=-1&&a[k][j]!=-1)              {                  if(a[i][j]>a[i][k]+a[k][j])                     {                        z++;                     }                  if(a[i][j]==a[i][k]+a[k][j]&&!b[i][j])                    {                       y++;                       b[i][j]=1;                    }               }              }          }           x=n*(n-1);           printf("Case %d: ",v);           if(z==0)           printf("%d\n",x-y);           else            printf("impossible\n");            v++;     }     return 0; }
代码不够优化,不过这一题数据比较少,可以过,如果把中间核心部分写成函数,中间直接return的话,时间复杂度
会低点。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值