码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-14 22:47:38      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:hdu   最短路   

Time Limit: 2000MS
Memory Limit: 32768KB
64bit IO Format: %I64d & %I64u

Status

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.
 

Source

HDU 2007-Spring Programming Contest - Warm Up (1)

思路:Floyd求最小环,搬模板

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

#define ll long long
const ll INF = 1<<30;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
int n, m;
const int MAXN = 110;
ll g[MAXN][MAXN];
ll dist[MAXN][MAXN];

int Floyd()
{
    ll mincircle = INF;
    for(int k = 1; k <= n; k++)
    {   //<span><span class="comment">求最小环权值</span></span>
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(i!=j && i!=k && j!=k && mincircle>dist[i][j]+g[i][k]+g[k][j])
                    mincircle = dist[i][j]+g[i][k]+g[k][j];
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(dist[i][j] > dist[i][k]+dist[k][j])
                    dist[i][j] = dist[i][k]+dist[k][j];
            }
        }
    }
    return mincircle;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(cin>>n>>m)
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                g[i][j] = (i==j)?0:INF;
            }
        }
        int p, q;
        ll w;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d %d %lld", &p, &q, &w);
            if(g[p][q] > w)
                g[p][q] = g[q][p] = w;
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                dist[i][j] = g[i][j];
            }
        }
        cout<<INF<<endl;
        ll ans = Floyd();
        if(ans != INF)
            printf("%lld\n", ans);
        else
            printf("It's impossible.\n");
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:hdu   最短路   

原文地址:http://blog.csdn.net/u014028317/article/details/47668245

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!