• HDU-3488 Tour


    最小权值环覆盖:拆点思想+求最小转求最大+KM算法

    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    #define rep(i, l, r) for(int i=l; i<=r; i++)
    #define clr(x, c) memset(x, c, sizeof(x))
    #define N 234
    #define MAX 1<<30
    #define ll long long
    using namespace std;
    int read()
    {
    	int x=0, f=1; char ch=getchar();
    	while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
    	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
    	return x*f;
    }
    int n, m, l[N], st[N], lx[N], ly[N], v[N][N];
    bool vx[N], vy[N];
    
    bool Find(int x)
    {
    	vx[x]=1;
    	rep(y, 1, n) if (!vy[y])
    	{
    		int a=lx[x]+ly[y]-v[x][y];
    		if (!a)
    		{
    			vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return true; }
    		}
    		else st[y]=min(st[y], a);
    	}
    	return false;
    }
    
    int km()
    {
    	rep(i, 1, n) lx[i]=-MAX; clr(ly, 0); clr(l, 0);
    	rep(i, 1, n) rep(j, 1, n) if (lx[i]<v[i][j]) lx[i]=v[i][j];
    	rep(i, 1, n)
    	{
    		rep(j, 1, n) st[j]=MAX; 
    		while (1)
    		{
    			clr(vx, 0); clr(vy, 0);
    			if (Find(i)) break; int a=MAX;
    			rep(j, 1, n) if (!vy[j] && st[j]<a) a=st[j];
    			rep(j, 1, n) if (vx[j]) lx[j]-=a;
    			rep(j, 1, n) if (vy[j]) ly[j]+=a; else st[j]-=a;
    		}
    	}
    	int ans=0;
    	rep(i, 1, n) if (!l[i] || v[l[i]][i]==-MAX) return -1; else ans+=v[l[i]][i];
    	return -ans;
    }
    
    int main()
    {
    	int t=read(); while (t--)
    	{
    		n=read(); m=read();
    		rep(i, 1, n) rep(j, 1, n) v[i][j]=MAX;
    		rep(i, 1, m) { int x=read(), y=read(), z=read(); if (v[x][y]>z) v[x][y]=z; }
    		rep(i, 1, n) rep(j, 1, n) v[i][j]*=-1;
    		printf("%d
    ", km());
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    企业IT管理说:全自动就一定是最好的吗?
    银行爱“IOE”爱得有多深
    谷歌数据中心
    屏蔽爬虫
    ,net运行框架
    视频格式
    HTTP协议
    IE的体系和webrowser
    jsp请求由servlet响应的方式
    Web请求响应简单整理
  • 原文地址:https://www.cnblogs.com/NanoApe/p/4382053.html
Copyright © 2020-2023  润新知