【 HDU 1853】Cyclic Tour 【MCMF or KM 解哈密顿环】

来源:互联网 发布:淘宝店铺一钻要卖多少 编辑:程序博客网 时间:2024/06/18 09:40

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
Sample Input
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
6 5
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
Sample Output
42
-1

Hint
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

题意,问是否存在哈密顿环,如果存在,找到一条最短的环。

哈密顿环除了起点,其它所有的点都是只能够经过一次。
费用流拆点

#include<bits/stdc++.h>using namespace std;#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = (100+10)*3;const int MAXM = 100+10;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;struct Edge {    int from,to,next,cap,flow,cost;}edge[MAXN*MAXN*2];int head[MAXN],top;void init(){    memset(head,-1,sizeof(head));    top=0;} void addedge(int a,int b,int w,int c){    edge[top].from=a,edge[top].to=b,edge[top].cap=w,edge[top].flow=0,edge[top].cost=c,edge[top].next=head[a];    head[a]=top++;        edge[top].from=b,edge[top].to=a,edge[top].cap=0,edge[top].flow=0,edge[top].cost=-c,edge[top].next=head[b];    head[b]=top++;}int n,m;int S,T;void getmap(){    S=0,T=n+n+1;    for(int i=1;i<=n;i++){        addedge(S,i,1,0);        addedge(i+n,T,1,0);    }    while(m--){        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        addedge(a,b+n,1,c);    }}bool vis[MAXN];int dis[MAXN];int pre[MAXN];bool spfa(int s,int e){    queue<int>Q;    memset(vis,0,sizeof(vis));    memset(dis,0x3f,sizeof(dis));    memset(pre,-1,sizeof(pre));    dis[s]=0;vis[s]=1;Q.push(s);    while(!Q.empty()){        int now=Q.front();Q.pop();vis[now]=0;        for(int i=head[now];i!=-1;i=edge[i].next){            Edge  e=edge[i];            if(e.cap>e.flow&&dis[e.to]>dis[now]+e.cost){                dis[e.to]=dis[now]+e.cost;                pre[e.to]=i;                if(!vis[e.to]){                    vis[e.to]=1;                    Q.push(e.to);                }            }        }    }    return pre[e]!=-1;}int MCMF(int s,int t,int &cost,int &flow){    flow=cost=0;    while(spfa(s,t)){        int Min=inf;        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){            Min=min(Min,edge[i].cap-edge[i].flow);         }         for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){             edge[i].flow+=Min;             edge[i^1].flow-=Min;             cost+=edge[i].cost*Min;         }         flow+=Min;     }}int main(){    CLOSE();//  fread();//  fwrite();    while(scanf("%d%d",&n,&m)!=EOF){        init();        getmap();        int flow,ans;        MCMF(S,T,ans,flow);        if(flow!=n)puts("-1");// 不是满流就不是哈密顿环        else         printf("%d\n",ans);    }    return 0;}

其实也可以转化为二分图最大权值匹配问题。
代码

#include<bits/stdc++.h>using namespace std;#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 300+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int match[MAXN]; int lx[MAXN],ly[MAXN]; int slack[MAXN]; int Map[MAXN][MAXN];int visx[MAXN],visy[MAXN];  int nx,ny;int n,m;void getmap(){     nx=ny=n;     for(int i=0;i<nx;i++){        for(int j=0;j<ny;j++)            Map[i][j]=-inf;     }     while(m--){        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        if(-c>Map[a-1][b-1]) // 有重边             Map[a-1][b-1]=-c;     }}int dfs(int x){    visx[x]=1;    for(int y=0;y<ny;y++){        if(visy[y]) continue;        int t=lx[x]+ly[y]-Map[x][y];        if(t==0){              visy[y]=1;            if(match[y]==-1||dfs(match[y])) {                match[y]=x;                return 1;            }        }else if(slack[y]>t)                  slack[y]=t;    }     return 0;}void KM(){    memset(match,-1,sizeof(match));    memset(ly,0,sizeof(ly));     for(int x=0;x<nx;x++){        lx[x]=-inf;          for(int j=0;j<ny;j++){            if(lx[x]<Map[x][j])                  lx[x]=Map[x][j];        }    }    for(int x=0;x<nx;x++){        for(int i=0;i<ny;i++) slack[i]=inf;        while(1){            memset(visx,0,sizeof(visx));            memset(visy,0,sizeof(visy));            if(dfs(x)) break;             int d=inf;              for(int i=0;i<ny;i++)                 if(!visy[i]) d=min(d,slack[i]);              for(int i=0;i<nx;i++)                 if(visx[i]) lx[i]-=d;            for(int i=0;i<ny;i++){                if(visy[i]) ly[i]+=d;                 else slack[i]-=d;               }         }    }    int flag=1; // 完美匹配    int ans=0;    for(int i=0;i<ny;i++){         if(match[i]==-1||Map[match[i]][i]==-inf){            flag=0;            break;         }          else  ans+=Map[match[i]][i];       }    if(flag) printf("%d\n",-ans);    else puts("-1");}int main(){    CLOSE();//  fread();//  fwrite();    while(scanf("%d%d",&n,&m)!=EOF){         getmap();         KM();    }    return 0;}
原创粉丝点击