hdu3488 Tour 拆点+二分图最佳匹配
2024-09-24 03:55:46
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.
题意:循环访问一个国家的所有城市,城市之间有边,求最小的花费
把所有城市拆成入点和出点,然后进行二分图最佳匹配即可。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int g[maxn][maxn],match[maxn],lx[maxn],ly[maxn],visx[maxn],visy[maxn],s[maxn],n,m;
bool hungary(int u){
visx[u]=;
for(int i=;i<=n;++i){
if(!visy[i]&&lx[u]+ly[i]==g[u][i]){
visy[i]=;
if(!match[i]||hungary(match[i])){
match[i]=u;
return ;
}
}
else if(!visy[i])s[i]=min(s[i],lx[u]+ly[i]-g[u][i]);
}
return ;
}
int KM(){
for(int i=;i<=n;++i){
for(int j=;j<=n;++j)s[j]=INF;
while(){
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(hungary(i))break;
int d=INF;
for(int j=;j<=n;++j)
if(!visy[j])d=min(d,s[j]);
for(int j=;j<=n;++j){
if(visx[j])lx[j]-=d;
if(visy[j])ly[j]+=d;
else s[j]-=d;
}
}
}
int ans=;
for(int i=;i<=n;++i)ans+=g[match[i]][i];
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(lx,,sizeof(lx));
memset(ly,,sizeof(ly));
memset(match,,sizeof(match));
memset(g,0xc0,sizeof(g));
for(int i=;i<=m;++i){
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
if(-v>g[a][b])g[a][b]=-v;
}
for(int i=;i<=n;++i){
for(int j=;j<=n;++j){
lx[i]=max(lx[i],g[i][j]);
}
}
printf("%d\n",-KM());
}
return ;
}
最新文章
- Git代码管理工具
- css overflow:hidden无效解决办法
- 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog
- Cocos2d-x中自定义粒子系统
- 【Hadoop代码笔记】通过JobClient对Jobtracker的调用详细了解Hadoop RPC
- 我的第一个Hibernate程序
- android:TextAppearance.Material.Widget.Button.Inverse找不到或者报错问题
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
- Gradle多项目配置的一个demo
- rhel 6.7 离线安装docker
- Lambda语言篇 —— lambda, 方法引用, 目标类型和默认方法
- [转]Java - 集合框架完全解析
- PHP XML SimpleXML
- iOS UITextField 响应键盘的return 事件
- Flutter之Color
- 2018上C语言程序设计(高级)- 第1次作业成绩
- MySQL官方教程及各平台的安装教程和配置详解入口
- 一起学Hive——总结复制Hive表结构和数据的方法
- js 遍历EL表达式 list对象
- json null