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 ;
}

最新文章

  1. Git代码管理工具
  2. css overflow:hidden无效解决办法
  3. 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog
  4. Cocos2d-x中自定义粒子系统
  5. 【Hadoop代码笔记】通过JobClient对Jobtracker的调用详细了解Hadoop RPC
  6. 我的第一个Hibernate程序
  7. android:TextAppearance.Material.Widget.Button.Inverse找不到或者报错问题
  8. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
  9. Gradle多项目配置的一个demo
  10. rhel 6.7 离线安装docker
  11. Lambda语言篇 —— lambda, 方法引用, 目标类型和默认方法
  12. [转]Java - 集合框架完全解析
  13. PHP XML SimpleXML
  14. iOS UITextField 响应键盘的return 事件
  15. Flutter之Color
  16. 2018上C语言程序设计(高级)- 第1次作业成绩
  17. MySQL官方教程及各平台的安装教程和配置详解入口
  18. 一起学Hive——总结复制Hive表结构和数据的方法
  19. js 遍历EL表达式 list对象
  20. json null

热门文章

  1. Win10系列:UWP界面布局进阶1
  2. Win10系列:JavaScript 的 WinJS库
  3. C++解析七-重载运算符和重载函数
  4. day10-高阶函数
  5. 用flask Flask-RESTful,实现RESTful API
  6. Python Select模型(程序流程)(转)
  7. leetcode python 004
  8. transclude
  9. DevExpress WinForms使用教程:Diagram Control
  10. JavaWeb基础-Session和Cookie