No minimum hoop FIG.

[Title Description:

Given an undirected graph, a drawing request comprises at least three points of the ring, the ring nodes do not overlap, and the ring on the side of the minimum length. This problem is called problem-free to the smallest ring of FIG. In this problem, you need the right edge of the output and the smallest ring. If there is no solution, output "No solution.". FIG nodes does not exceed 100.

[Input Description:

The first line of two positive integers n, m represents the number of points and edges.

Next m lines of three positive integers x, y, z, represents the node x, y has a length between the edge of z.

[Output] Description:

A minimum ring and the output side of the right. If there is no solution, output "No solution."

[Sample input]:

5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20

[] Sample output:

61

[Time limit, and the range of data Description:

Time: 1s space: 512M

For 20% of the data: 1 <= n <= 10;

To 100% of the data: 1 <= n <= 100; Right side <= 300.

 

analysis:

This question requires a more profound understanding for floyd, seeking ring is obviously the most important part of this problem, and how to find the ring requires knowledge of floyd, if i -> j has one path, and i -> k -> j smaller, then this is a ring of. The total weight was then recorded for each ring, it can be the minimum value.

 

CODE:

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int M=105;
 6 const int inf=1<<10;
 7 int n,m;
 8 int a[M][M];
 9 int f[M][M];
10 int ans=inf;
11 inline int get(){
12     char c=getchar();
13     int res=0;
14     while (c<'0'||c>'9') c=getchar();
15     while (c>='0'&&c<='9'){
16         res=(res<<3)+(res<<1)+c-'0';
17         c=getchar();
18     }
19     return res;
20 }
21 int main(){
22     n=get(),m=get();
23     for (int i=1;i<=n;i++)
24         for (int j=1;j<=n;j++)
25             a[i][j]=inf,f[i][j]=inf;
26     for (int i=1;i<=m;i++){
27         int x,y,z;
28         x=get();y=get();z=get();
29         a[x][y]=a[y][x]=f[x][y]=f[y][x]=z;
30     }
31     for (int k=1;k<=n;k++){
32         for (int i=1;i<=n;i++){
33             for (int j=1;j<=n;j++){
34                 if (i==j||j==k||i==k) continue;
35                 ans=min(ans,a[k][j]+a[i][k]+f[i][j]);
36                 f[i][j]=min(f[i][j],f[i][k]+f[j][k]);
37             }
38         }
39     }
40     if (ans==inf) cout<<"No solution."<<endl; 
41     else cout<<ans<<endl;
42     return 0;
43 }

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11119990.html