HDU 1853 Cyclic Tour && HDU 3488 Tour 最小费用流

Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2490    Accepted Submission(s): 1228


Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
 

Input
An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
 

Output
For each test case, output a line with exactly one integer, which is the minimum total distance.
 

Sample Input
  
  
1 6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4
 

Sample Output

42

//给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环,环中至少有两个顶点,且所有定点都只被一个有向环覆盖.问你该有向环所有权值的总和最小是多少?(保证有解)
//把每个点拆成两个,i和i+n,其中源点和i连边,容量1费用0,i+n和汇点连边,容量1费用0,如果点a和点b有通路时,那么连接a和b+n,费用为边的长度。当流量为n时,则说明图上所有点都被覆盖,套模板即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int N = 500;
const int INF = 0x3f3f3f3f;
struct edge
{
    int st, to, cap, cost, next;
}g[N*1000];
int dis[N], head[N], pre[N], mpa[N][N];
int cnt;
bool used[N];

void add_edge(int v, int u, int cap, int cost)
{
    g[cnt].st = v, g[cnt].to = u, g[cnt].cap = cap, g[cnt].cost = cost, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].st = u, g[cnt].to = v, g[cnt].cap = 0, g[cnt].cost = -cost, g[cnt].next = head[u], head[u] = cnt++;
}
int min_cost_flow(int s, int t, int f)
{
    int res = 0;
    while(f > 0)
    {
        memset(dis, 0x3f, sizeof dis);
        memset(used, 0, sizeof used);
        memset(pre, -1, sizeof pre);
        queue <int> que;
        que.push(s);
        dis[s] = 0;
        used[s] = true;
        while(! que.empty())
        {
            int v = que.front(); que.pop();
            for(int i = head[v]; i != -1; i = g[i].next)
            {
                int u = g[i].to;
                if(g[i].cap > 0 && dis[u] > dis[v] + g[i].cost)
                {
                    dis[u] = dis[v] + g[i].cost;
                    pre[u] = i;
                    if(!used[u])
                    {
                        que.push(u);
                        used[u] = true;
                    }
                }
            }
            used[v] = false;
        }
        if(dis[t] == INF) return -1;
        int d = f;
        for(int i = pre[t]; i != -1; i = pre[g[i].st])
            d = min(d, g[i].cap);
        f -= d;
        res += d * dis[t];
        for(int i = pre[t]; i != -1; i = pre[g[i].st])
            g[i].cap -= d, g[i^1].cap += d;
    }
    return res;
}
int main()
{
    int t, n, m, a, b, c;
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        memset(mpa, 0x3f, sizeof mpa); //mpa数组用来去重边,不去会T
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++)
        {
            add_edge(0, i, 1, 0);
            add_edge(i + n, 2 * n + 1, 1, 0);
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            mpa[a][b] = min(mpa[a][b], c);
        }
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(mpa[i][j] != INF)
                    add_edge(i, n + j, 1, mpa[i][j]);
        printf("%d\n", min_cost_flow(0, 2 * n + 1, n));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值