Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1266 Accepted Submission(s): 666
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.
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.
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
Source
Recommend
zhouzeyong
题目大体意思是求n个环的并,每个点只能在一个环里,找到可以遍历所有顶点的边的最小值,显然每个点只能关联其他两个顶点,并且入度和初度都为1,所以应该是完全匹配,完全匹配图就是n个环的并,所以求的是完美匹配中的最小权问题,用km解决即可~
KM算法详解见:http://www.cnblogs.com/jackge/archive/2013/05/03/3057028.html
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int VM=250; const int INF=0x3f3f3f3f; int n,m,nx,ny; int linker[VM],lx[VM],ly[VM],slack[VM]; int visx[VM],visy[VM],w[VM][VM]; int DFS(int x){ visx[x]=1; for(int y=1;y<=ny;y++){ if(visy[y]) continue; int tmp=lx[x]+ly[y]-w[x][y]; if(tmp==0){ visy[y]=1; if(linker[y]==-1 || DFS(linker[y])){ linker[y]=x; return 1; } }else if(slack[y]>tmp){ slack[y]=tmp; } } return 0; } int KM(){ int i,j; memset(linker,-1,sizeof(linker)); memset(ly,0,sizeof(ly)); for(i=1;i<=nx;i++) for(j=1,lx[i]=-INF;j<=ny;j++) if(w[i][j]>lx[i]) lx[i]=w[i][j]; for(int x=1;x<=nx;x++){ for(i=1;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(i=1;i<=ny;i++) if(!visy[i] && d>slack[i]) d=slack[i]; for(i=1;i<=ny;i++) if(visx[i]) lx[i]-=d; for(i=1;i<=ny;i++) if(visy[i]) ly[i]+=d; else slack[i]-=d; } } int res=0; for(i=1;i<=ny;i++) if(linker[i]!=-1) res+=w[linker[i]][i]; return -res; } int main(){ //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); nx=ny=n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) w[i][j]=-INF; //即将边的权值取反 int u,v,cap; while(m--){ scanf("%d%d%d",&u,&v,&cap); if(w[u][v]<-cap) w[u][v]=-cap; } printf("%d\n",KM()); } return 0; }
相关推荐
- **【HDU 3488】Tour 最小费用圈覆盖**、**【HDU 3435】A new Graph Game**、**【HDU 3722】Card Game**、**【HDU 3718】Similarity 求相似度**:这些题目虽然不是直接与KMP算法相关,但标签中的“最小费用圈覆盖”...
那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
麒麟win10双系统重新安装win10后麒麟启动菜单看不到解决方法
多邻国Duolingo v6.0.3 高级版.apk
QT网络编程: 实现TCP通讯设置(客户端)
减少重复造轮子,开源微信小程序商城(前后端开源:uniapp+Java)。快速搭建一个属于自己的微信小程序商城。
彩虹云商城 最新彩虹代刷V6.9.0免授权纯净完整版 直接上传源码解压缩后访问域名安装即可,亲测可用 彩虹自助下单系统 安装说明: 上传到空间后直接访问即可根据提示安装。 PHP推荐使用7.0及以上版本 V6.9 1.修复SQL注入漏洞 2.修复后台微信QQ扫码登录 V6.8.5 1.修复亿乐对接 2.新增支持倍数输入框 V6.8 1.更新全新的faka模板 2.新增微信快捷登录 3.新增批量下单功能 4.防CC配置新增滑动验证码模式 5.修复部分地区后台加载错误 6.修复https网站对接http支付接口 7.后台登录支持微信QQ扫码登录
MyBatis-Plus学习思维导图
那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
分布式搜索引擎ElasticSearch思维导图
网鼎杯
网络安全入门教程(工具版)
科普里控制器调试软件工具使用 win64环境安装
内容概要:本文档详细介绍了GC9503V单片机a-Si TFT LCD驱动器的技术规格,包括主要特点、内部结构图、引脚定义以及系统接口等。GC9503V支持480x864分辨率,16.7百万色显示,无内置GRAM。文章还提供了详细的引脚尺寸、对齐标记尺寸、芯片信息以及接口模式控制的序列实例,如DCS写入命令及其参数。 适合人群:LCD显示屏设计人员、嵌入式系统工程师、电子硬件开发者和技术研究人员。 使用场景及目标:帮助开发者快速理解和应用GC9503V在实际产品中的具体使用方法,掌握LCM与MCU之间的数据交互方式,实现高效的屏幕驱动设计。 其他说明:GalaxyCore公司保留在不事先通知的情况下更改文档内容的权利。
那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
yolo系列算法目标检测数据集,包含标签,可以直接训练模型和验证测试,数据集已经划分好,适用yolov5,yolov8,yolov9,yolov7,yolov10,yolo11算法; 包含两种标签格:yolo格式(txt文件)和voc格式(xml文件),分别保存在两个文件夹中; yolo格式:<class> <x_center> <y_center> <width> <height>, 其中: <class> 是目标的类别索引(从0开始)。 <x_center> 和 <y_center> 是目标框中心点的x和y坐标,这些坐标是相对于图像宽度和高度的比例值,范围在0到1之间。 <width> 和 <height> 是目标框的宽度和高度,也是相对于图像宽度和高度的比例值
那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
某酒店排水课程设计计算书.doc
那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据