最小环 hdu1599 poj1734

  最小环用floyd改编。

  hdu1599特殊一些。要求至少有三个不同的点,并且除了起点与终点重合外,中间不能有环。有点很奇怪,最大值不能为0x3f3f3f3f。

  poj1374就没那么讲究。

  

 1 //hdu1599
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 const int N = 110, INF=1000000;
 8 int Map[N][N], dist[N][N], pre[N][N];
 9 int mc;
10 void fc(int n)
11 {
12     int i,j,k;
13     mc=INF;
14     for(i=1;i<=n;i++)
15     {
16         for(j=1;j<=n;j++)
17         {
18             dist[i][j]=Map[i][j];
19             pre[i][j]=i;
20         }
21     }
22     for(k=1;k<=n;k++)
23     {
24         for(i=1;i<k;i++)
25         {
26             for(j=1;j<i;j++)
27             {
28                 if(dist[i][j]+Map[k][j]+Map[i][k]<mc)
29                     mc=min(mc,dist[i][j]+Map[k][j]+Map[i][k]);
30             }
31         }
32         for(i=1;i<=n;i++)
33         {
34             for(j=1;j<=n;j++)
35             {
36                 if(dist[k][j]!=INF&&dist[i][k]!=INF&&dist[i][j]>dist[i][k]+dist[k][j])
37                 {
38                     dist[i][j]=dist[i][k]+dist[k][j];
39                     pre[i][j]=pre[k][j];
40                 }
41             }
42         }
43     }
44 }
45 void init(int n)
46 {
47     for(int i=0;i<=n;i++)
48     {
49         Map[i][i]=0;
50         for(int j=0;j<i;j++)
51             Map[j][i]=Map[i][j]=INF;
52     }
53 }
54 int main()
55 {
56     //freopen("test.txt","r",stdin);
57     int n,m,i,j,k;
58     while(scanf("%d%d",&n,&m)!=EOF)
59     {
60         init(n);
61         while(m--)
62         {
63             scanf("%d%d%d",&i,&j,&k);
64             if(i==j) continue;
65             Map[i][j]=Map[j][i]=min(Map[i][j],k);
66         }
67         fc(n);
68         if(mc!=INF) printf("%d\n",mc);
69         else printf("It's impossible.\n");
70     }
71     return 0;
72 }
View Code

 

下面是poj1374

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int N=101, INF=0x3f3f3f3f;
 8 int Map[N][N], dist[N][N], pre[N][N];
 9 int mc, p[N], t, n;
10 void fc()
11 {
12     int i,j,k;
13     mc=INF;
14     t=0;
15     for(i=1;i<=n;i++)
16     {
17         for(j=1;j<=n;j++)
18         {
19             dist[i][j]=Map[i][j];
20             pre[i][j]=i;
21         }
22     }
23     for(k=1;k<=n;k++)
24     {
25         for(i=1;i<=n;i++)
26         {
27             if(Map[k][i]==INF) continue;
28             if(i==k) continue;
29             for(j=1;j<=n;j++)
30             {
31                 if(i==j||j==k) continue;
32                  if(dist[i][j]==INF||Map[j][k]==INF) continue;
33                 int temp=dist[i][j]+Map[i][k]+Map[k][j];
34                 if(temp<mc)
35                 {
36                     mc=temp;
37                     int x=j;
38                     t=0;
39                     while(x!=i)
40                     {
41                         p[t++]=x;
42                         x=pre[i][x];
43                     }
44                     p[t++]=i;
45                     p[t++]=k;
46                 }
47             }
48         }
49         for(i=1;i<=n;i++)
50         {
51             if(dist[i][k]==INF) continue;
52             for(j=1;j<=n;j++)
53             {
54                 if(dist[i][j]>dist[i][k]+dist[k][j])
55                 {
56                     dist[i][j]=dist[i][k]+dist[k][j];
57                     pre[i][j]=pre[k][j];
58                 }
59             }
60         }
61     }
62 }
63 int main()
64 {
65     int i,j,k,m;
66     //freopen("test.txt","r",stdin);
67     //freopen("out.txt","w",stdout);
68     while(cin>>n>>m)
69     {
70         for(i=1;i<=n;i++)
71             for(j=1;j<=n;j++)
72                 Map[i][j]=INF;
73         while(m--)
74         {
75             scanf("%d%d%d",&i,&j,&k);
76             Map[j][i]=Map[i][j]=k<Map[i][j]?k:Map[i][j];
77         }
78         fc();
79         if(mc==INF) printf("No solution.\n");
80         else
81         {
82             printf("%d",p[0]);
83             for(i=1;i<t;i++) printf(" %d",p[i]);
84             printf("\n");
85         }
86     }
87     return 0;
88 }
View Code

 

posted @ 2014-09-03 17:05  pengmq  阅读(649)  评论(0编辑  收藏  举报