[HDU1599]find the mincost route

Description

[HDU1599]find the mincost route

Solution

恶补图论,最小环问题的板子题
\(floyd\)来判最小环,复杂度\(O(n^3)\)
枚举\(k\)从\(1\)到\(n\)
最小环的\(i\)从\(1\)到\(k-1\),\(j\)从\(1\)到\(i-1\)
令\(ans=min(ans,f[i][j]+a[i][k]+a[k][j])\)
同时维护最短路数组的\(i\)从\(1\)到\(n\),\(j\)从\(1\)到\(n\)

Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x7ffffff
#define MAXN 110
ll n, m, x, y, z, ans;
ll a[MAXN][MAXN], f[MAXN][MAXN];
inline ll read() {
    ll s = 0, w = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
    for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
    return s * w;
}
int main() {
    while (scanf("%lld%lld", &n, &m) == 2) {
        for (register ll i = 1; i <= n; i++)
            for (register ll j = 1; j <= n; j++)
                a[i][j] = f[i][j] = INF;
        for (register ll i = 1; i <= m; i++) {
            x = read(), y = read(), z = read();
            a[x][y] = a[y][x] = min(a[x][y], z);
            f[x][y] = f[y][x] = min(f[x][y], z);
        }
        ans = INF;
        for (register ll k = 1; k <= n; k++) {
            for (register ll i = 1; i < k; i++)
                for (register ll j = i + 1; j < k; j++)
                    ans = min(ans, f[i][j] + a[i][k] + a[k][j]);
            for (register ll i = 1; i <= n; i++)
                for (register ll j = 1; j <= n; j++)
                    f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
        }
        if (ans == INF)
            printf("It's impossible.\n");
        else
            printf("%lld\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Agakiss/p/11808713.html

时间: 2024-08-27 09:02:18

[HDU1599]find the mincost route的相关文章

hdu1599 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): 3456    Accepted Submission(s): 1409 Problem Description 杭 州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为

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

find the mincost route Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个

hdu 1599 find the mincost route

http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200 5 using namespace std; 6 const int inf=1<<28; 7 8 int g[maxn][maxn],dis[maxn][maxn]; 9 int

hdoj 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): 3273    Accepted Submission(s): 1320 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1

hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2801    Accepted Submission(s): 1

hdu 1599 find the mincost route (最小环与floyd算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2530    Accepted Submission(s): 1006 Problem Description 杭州有N个景区,景区之间有一

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): 2973    Accepted Submission(s): 1197 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V

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): 3885    Accepted Submission(s): 1559 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1

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): 3425    Accepted Submission(s): 1397 Problem Description 杭 州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为