HDU3488 Tour [有向环覆盖 费用流]

本文介绍了一个旅行路线设计问题,目标是最小化总旅行距离的同时确保每个城市恰好被访问一次并形成一个或多个环路。通过构建二分图并求解最小权最大匹配问题来解决该问题。

Tour

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

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
 
Source

和DAG的最小路径覆盖很像 http://www.cnblogs.com/candy99/p/6115989.html
也是拆入点和出点建二分图,然后求最小权最大匹配,因为保证有解所以一定是一个完美匹配,这个费用就是答案了
 
此题时间太烦了,必须要先去重边..............................
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=505,M=1e5,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n,m,u,v,w,s,t;
struct edge{
    int v,ne,c,f,w;
}e[M<<1];
int cnt,h[N];
inline void ins(int u,int v,int c,int w){
    cnt++;
    e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].w=w;
    e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].c=0;e[cnt].f=0;e[cnt].w=-w;
    e[cnt].ne=h[v];h[v]=cnt;
}
int d[N],pre[N],pos[N],q[N],head=1,tail=1,inq[N];
inline void lop(int &x){if(x==N) x=1;else if(x==0) x=N-1;}
bool spfa(){
    memset(d,127,sizeof(d));
    d[s]=0;pre[t]=-1;
    head=tail=1;
    memset(inq,0,sizeof(inq));
    q[tail++]=s;inq[s]=1;
    while(head!=tail){
        int u=q[head++];lop(head);inq[u]=0;
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(d[v]>d[u]+w&&e[i].c>e[i].f){
                d[v]=d[u]+w;
                pre[v]=u;
                pos[v]=i;
                if(!inq[v]){
                    if(d[v]<d[q[head]]) head--,lop(head),q[head]=v;
                    else q[tail++]=v,lop(tail);
                    inq[v]=1;
                }
            }
        }
    }
    return pre[t]==-1?0:1;
}
int mcmf(){
    int flow=0,cost=0;
    while(spfa()){
        int f=INF;
        for(int i=t;i!=s;i=pre[i]) f=min(f,e[pos[i]].c-e[pos[i]].f);
        flow+=f;
        cost+=f*d[t];
        for(int i=t;i!=s;i=pre[i]){
            e[pos[i]].f+=f;
            e[((pos[i]-1)^1)+1].f-=f;
        }
    }
    return cost;
}

int g[N][N];
int main(){
    //freopen("in.txt","r",stdin);
    int T=read();
while(T--){
    cnt=0;
    memset(h,0,sizeof(h));
    n=read();m=read();s=0;t=n+n+1;
    for(int i=1;i<=n;i++){
        ins(s,i,1,0),ins(n+i,t,1,0);
        for(int j=1;j<=n;j++) g[i][j]=INF;
    }
    
    for(int i=1;i<=m;i++) u=read(),v=read(),w=read(),g[u][v]=min(g[u][v],w);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) 
            if(g[i][j]<INF) ins(i,j+n,1,g[i][j]);
    printf("%d\n",mcmf());
}
}

 

 
 

转载于:https://www.cnblogs.com/candy99/p/6347825.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值