hdu 4034 Graph

 1
http://acm.hdu.edu.cn/showproblem.php?pid=4034
 vis[][]作用是 防止一条边被删多次
2
3 #include<stdio.h>
4 #include<string.h>
5 #define N 1000
6 int ans;
7 int map[N][N],n,f,vis[N][N];
8 void search()
9 {
10 int i,j,k;
11 memset(vis,0,sizeof(vis));
12 for(k=1;k<=n;k++)
13 {
14 for(i=1;i<=n;i++)
15 {
16 if(i==k)continue;
17 for(j=1;j<=n;j++)
18 {
19 if(i==j||j==k)continue;
20 if(map[i][j]==map[i][k]+map[k][j]&&!vis[i][j])
21 {
22 //printf("%d %d %d \n",i,k,j);
23 ans--;
24 vis[i][j]=1;
25
26 }
27 if(map[i][j]>map[i][k]+map[k][j])
28 {
29 f=1;
30 return ;
31
32 }
33
34
35 }
36 }
37 }
38 }
39 int main()
40 {
41 int T;
42 int l,i,j;
43 scanf("%d",&T);
44 for(l=1;l<=T;l++)
45 {
46 scanf("%d",&n);
47 for(i=1;i<=n;i++)
48 for(j=1;j<=n;j++)
49 scanf("%d",&map[i][j]);
50 ans=(n-1)*n;
51 printf("Case %d: ",l);
52 f=0;
53 search();
54
55 if(!f)
56 printf("%d\n",ans);
57 else printf("impossible\n");
58
59 }
60
61 }
 

  

你可能感兴趣的:(Graph)