HDU 3488 Tour (最小费用流 或 KM)

Tour

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


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
 

题意:给一个顶点为n(<=200)的有向图,每条边都是有一个距离。问使所有的点都在哈密顿环上,边权总和最小。一个点只属于一个环,且只能走一次。可以有一个或多个环。

解题:一个哈密顿回路上的点,都只有一个出度一个入度。通过拆点(i, i+n),变成二部图。  一个点出,一个点入,且自身拆的点没有边连。如果是用网络流则一定是满流。这里用可以用最小费用最大流来解。边容都为1,花费为两点间的距离。还有就是要去重不然会TLE。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN = 10010;
const int MAXM = 100100;
const int INF = 1<<30;
struct EDG{
    int to,next,cap,flow;
    int cost;  //每条边的单价
}edg[MAXM];
int head[MAXN],eid;
int pre[MAXN], cost[MAXN]  ; //点0~(n-1)

void init(){
    eid=0;
    memset(head,-1,sizeof(head));
}
void addEdg(int u,int v,int cap,int cst){
    edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst;
    edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++;

    edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst;
    edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;
}

bool inq[MAXN];
bool spfa(int sNode,int eNode,int n){
    queue<int>q;
    for(int i=0; i<n; i++){
        inq[i]=false; cost[i]= INF;
    }
    cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1;
    q.push(sNode);
    while(!q.empty()){
        int u=q.front(); q.pop();
        inq[u]=0;
        for(int i=head[u]; i!=-1; i=edg[i].next){
            int v=edg[i].to;
            if(edg[i].cap-edg[i].flow>0 && cost[v]>cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费
                cost[v] = cost[u]+edg[i].cost;
                pre[v]=i;   //记录路径上的边
                if(!inq[v])
                    q.push(v),inq[v]=1;
            }
        }
    }
    return cost[eNode]!=INF;    //判断有没有增广路
}
//反回的是最大流,最小花费为minCost
int minCost_maxFlow(int sNode,int eNode ,int& minCost,int n){
    int ans=0;
    while(spfa(sNode,eNode,n)){
        ans++;
        for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){
            edg[i].flow+=1; edg[i^1].flow-=1;
            minCost+=edg[i].cost;
        }
    }
    return ans;
}
void scanf(int &ans){
    char ch;
    while(ch=getchar()){
        if(ch>='0'&&ch<='9')
            break;
    }
    ans=ch-'0';
    while(ch=getchar()){
        if(ch<'0'||ch>'9')
            break;
        ans=ans*10+ch-'0';
    }
}
int main(){
    int T,n,m , u, v, d , mapt[205][205];
    scanf(T);
    while(T--){
        scanf(n); scanf(m);
        init();
        int s=0, t=2*n+1;

        for(int i=1; i<=n; i++){
            addEdg(s , i , 1 , 0);
            addEdg(i+n , t , 1 , 0);
            for(int j=1; j<=n; j++)
                mapt[i][j]=INF;
        }
        while(m--){
            scanf(u); scanf(v); scanf(d);
            if(mapt[u][v]>d)
                mapt[u][v]=d;
        }
        for( u=1; u<=n; u++)
            for(v=1; v<=n; v++)
            if(mapt[u][v]!=INF)
                addEdg(u,v+n,1,mapt[u][v]);
        
        int mincost=0;
        minCost_maxFlow(s , t , mincost , t+1);

        printf("%d\n",mincost);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值