HDU-1599 find the mincost route(floyd求最小环)

find the mincost route

Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4468    Accepted Submission(s): 1798


Problem Description
杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。
 
Input
第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
 
Output
对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It's impossible.".
 
Sample Input
3 3 1 2 1 2 3 1 1 3 1 3 3 1 2 1 1 2 3 2 3 1
 
Sample Output
3 It's impossible.
 
Author
8600
 
Source
 
Recommend
8600   |   We have carefully selected several similar problems for you:  1595 1598 1596 1142 1217 
 
古人云:熟能生巧,善哉善哉
 1 #include "bits/stdc++.h"
 2 #define mem(a,b) memset(a,b,sizeof(a))
 3 using namespace std;
 4 typedef long long LL;
 5 const int MAX=105;
 6 const int oo=100000000;
 7 int ans;
 8 int n,m;
 9 int dist[MAX][MAX],a[MAX][MAX];
10 inline int mn(int x,int y){return x<y?x:y;}
11 void floyd(){
12     ans=oo;
13     int i,j,k;
14     for (k=1;k<=n;k++){
15         for (i=1;i<k;i++){
16             for (j=i+1;j<k;j++){
17                 ans=mn(ans,a[i][k]+a[k][j]+dist[i][j]);
18             }
19         }
20         for (i=1;i<=n;i++){
21             for (j=1;j<=n;j++){
22                 dist[i][j]=mn(dist[i][j],dist[i][k]+dist[k][j]);
23             }
24         }
25     }
26 }
27 int main(){
28     freopen ("find.in","r",stdin);
29     freopen ("find.out","w",stdout);
30     int i,j;
31     int u,v,w;
32     while (~scanf("%d%d",&n,&m)){
33         for (i=1;i<=n;i++) for (j=1;j<=n;j++) dist[i][j]=a[i][j]=(i==j?0:oo);
34         for (i=1;i<=m;i++){
35             scanf("%d%d%d",&u,&v,&w);
36             if (w<a[u][v]) 
37              a[u][v]=a[v][u]=dist[u][v]=dist[v][u]=w;
38         }
39         floyd();
40         if (ans>=oo)
41          puts("It's impossible.");
42         else printf("%d
",ans);
43     }
44     return 0;
45 }
think by yourself !
未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
原文地址:https://www.cnblogs.com/keximeiruguo/p/6055289.html