hdu 1599 find the mincost route

find the mincost route

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


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
 
题意:有N个景区, M条路, 要从一个景区出发然后再回到开始的景区, 要求话费最少。
思路: 无向图最小环. 模版体

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int v = 105;
const int inf = 100000000;
int n, m, map[v][v], ans, dist[v][v];
int main() {
    while(~scanf("%d%d", &n, &m)) {
        ans = inf;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                map[i][j] = dist[i][j] = (i == j) ? 0 : inf;
        while(m--) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            map[a][b] = map[b][a] = dist[a][b] = dist[b][a] = min(c, map[a][b]);
        }
        for(int k = 1; k <= n; ++k) {
            for(int i = 1; i < k; ++i)
                for(int j = i + 1; j < k; ++j)
                    ans = min(ans, dist[i][j] + map[k][i] + map[k][j]);
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= n; ++j)
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        }
        if(ans < inf)
            printf("%d\n", ans);
        else
            printf("It's impossible.\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值