zoukankan      html  css  js  c++  java
  • Tour(KM算法)

    Tour

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

    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
     

    题意:让找一个环,费用最小,这个环要包括所有的点,km算法;不过要建负边;负的最大匹配等于最小匹配,而且要考虑重边的情况;大神们好多用费用流写的。。。膜拜;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const double PI=acos(-1.0);
    #define mem(x,y) memset(x,y,sizeof(x))
    typedef long long LL;
    #define SI(x) scanf("%d",&x)
    #define SL(x) scanf("%lld",&x)
    #define T_T while(T--)
    #define F(i,x) for(i=1;i<=x;i++)
    #define PR(x) printf("%d",x)
    #define PL(x) printf("%lld",x)
    #define p_ printf(" ")
    const int MAXN=210;
    const int MAXM=30010;
    int mp[MAXN][MAXN];
    int lx[MAXN],ly[MAXN],usdx[MAXN],usdy[MAXN],link[MAXN];
    int N;
    bool dfs(int x){
    	int i,j;
    	usdx[x]=1;
    	F(i,N){
    		if(!usdy[i]&&lx[x]+ly[i]==mp[x][i]){
    			usdy[i]=1;
    			if(link[i]==-1||dfs(link[i])){
    				link[i]=x;return true;
    			}
    		}
    	}
    	return false;
    }
    int km(){
    	mem(ly,0);mem(link,-1);
    	int i,j,k;
    	F(i,N){
    		lx[i]=-INF;
    		F(j,N){
    			lx[i]=max(lx[i],mp[i][j]);
    		}
    	}
    	F(i,N){
    		mem(usdx,0);mem(usdy,0);
    		while(!dfs(i)){
    			int d=INF;
    			F(j,N){
    				if(usdx[j]){
    					F(k,N)
    						if(!usdy[k])
    							d=min(d,lx[j]+ly[k]-mp[j][k]);
    				}
    			}
    			F(j,N){
    				if(usdx[j])lx[j]-=d;
    				if(usdy[j])ly[j]+=d;
    			}
    			mem(usdx,0);mem(usdy,0);
    		}
    	}
    	int ans=0;
    	F(i,N)ans+=lx[i]+ly[i];
    	return -ans;
    }
    void initial(){
    	int i,j;
    	F(i,N)F(j,N)mp[i][j]=-INF;
    }
    int main(){
    	int T,M;
    	SI(T);
    	T_T{
    		int a,b,c;
    		SI(N);SI(M);
    		initial();
    		while(M--){
    			SI(a);SI(b);SI(c);
    			if(-c>mp[a][b])mp[a][b]=-c;
    		}
    		printf("%d
    ",km());
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    帧同步与状态同步
    撸个服务端出来系列(五)
    撸个服务端出来系列(四)
    撸个服务端出来系列(三)
    撸个服务端出来系列(二)
    新博客手机适配的问题
    撸个服务端出来系列(一)
    高性能事件分发器,lua 版
    cocos2dx lua UI栈
    cocos2dx3.x+cocostudio多屏幕分辨率适配解决方案(干货)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4999283.html
Copyright © 2011-2022 走看看