Graph_hdu_4034 (delete edge). java

Source: Internet
Author: User

GraphTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission (s): 1587 Accepted Submission (s ): 790 Problem DescriptionEveryone 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? InputThe 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. outputFor 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 Input330 1 11 0 11 1 030 1 3 4 0 27 3 030 1 41 0 24 2 0 Sample OutputCase 1: 6 Case 2: 4 Case 3: impossible SourceThe 36th ACM/ICPC Asia Regional Chengdu Site -- Online Contest Recommendlcy | We have carefully selected several similar problems for you: 4039 4038 4036 4033 4037

import java.io.InputStreamReader;  import java.util.Scanner;    public class Main{      public static void main(String[] args) {          Scanner input=new Scanner(new InputStreamReader(System.in));          int N=input.nextInt();          int e=1;          while(N-->0){              int n=input.nextInt();              int a[][]=new int[n+1][n+1];              for(int i=1;i<=n;i++){                  for(int j=1;j<=n;j++){                      a[i][j]=input.nextInt();                  }              }              boolean ok=false;              for(int i=1;i<=n;i++){                  for(int j=1;j<=n;j++){                      if(i==j)                          continue;                      for(int k=1;k<=n;k++){                          if(j==k)                              continue;                          if(a[k][i]!=0&&a[i][j]!=0){                              if(a[k][i]+a[i][j]==a[k][j])                                  a[k][j]=0;                              else if(a[k][i]+a[i][j]<a[k][j]){                                  System.out.println("Case "+(e++)+": impossible");                                  ok=true;                                  break;                              }                          }                      }                      if(ok)                          break;                  }                  if(ok)                      break;              }              if(ok)                  continue;              int sum=0;              for(int i=1;i<=n;i++){                  for(int j=1;j<=n;j++){                      if(a[i][j]!=0)                          sum++;                  }              }              System.out.println("Case "+(e++)+": "+sum);          }      }  }  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.