hdoj--3488--Tour(KM)
Tour
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.
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.
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
42
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int map[210][210];
int slock[210];
int lx[210],ly[210];
bool visx[210],visy[210];
int nx,ny,match[210];
void getmap()
{
scanf("%d%d",&n,&m);
nx=ny=n;
for(int i=1;i<=nx;i++)
for(int j=1;j<=ny;j++)
map[i][j]=-INF;
int x,y,w;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&w);
if(-w>map[x][y])
map[x][y]=-w;
}
}
int DFS(int x)
{
visx[x]=true;
for(int y=1;y<=ny;y++)
{
if(visy[y]) continue;
int t=lx[x]+ly[y]-map[x][y];
if(t==0)
{
visy[y]=true;
if(match[y]==-1||DFS(match[y]))
{
match[y]=x;
return 1;
}
}
else if(t<slock[y])
slock[y]=t;
}
return 0;
}
void KM()
{
memset(match,-1,sizeof(match));
memset(ly,0,sizeof(ly));
for(int x=1;x<=nx;x++)
{
lx[x]=-INF;
for(int y=1;y<=ny;y++)
lx[x]=max(lx[x],map[x][y]);
}
for(int x=1;x<=nx;x++)
{
for(int y=1;y<=ny;y++)
slock[y]=INF;
while(1)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x)) break;
int d=INF;
for(int i=1;i<=ny;i++)
{
if(!visy[i]&&slock[i]<d)
d=slock[i];
}
for(int i=1;i<=nx;i++)
{
if(visx[i])
lx[i]-=d;
}
for(int i=1;i<=ny;i++)
{
if(visy[i])
ly[i]+=d;
else
slock[i]-=d;
}
}
}
int ans = 0;
for(int i = 1;i <= ny; i++)
ans += map[match[i]][i];
printf("%d\n",-ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
getmap();
KM();
}
return 0;
}
hdoj--3488--Tour(KM)的更多相关文章
- HDU - 3488 Tour (KM最优匹配)
题意:对一个带权有向图,将所有点纳入一个或多个环中,且每个点只出现一次,求其所有环的路径之和最小值. 分析:每个点都只出现一次,那么换个思路想,每个点入度出度都为1.将一个点拆成两个点,一个作为入度点 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- hdoj 3488 Tour 【最小费用最大流】【KM算法】
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submi ...
- HDU 3488 Tour(最小费用流:有向环最小权值覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...
- HDU3488:Tour(KM算法)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- 【UVA 1411】 Ants (KM)
Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...
- 【HDU 2853】Assignment (KM)
Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...
- 奔小康赚大钱(km)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU ACM 1224 Free DIY Tour (SPFA)
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- Laravel5.1 学习笔记1, 目录结构和命名空间(待修)
自从用 Laravel4做了个小网站,使用了数据库ORM Eloquent, 就放下了一段时间,想不到这个与Asp.net MVC 有着异曲同工之妙的框架已经出了下个版本,而且还有不小的改动,因此不得 ...
- 时序分析:HMM模型(状态空间)
关于HMM模型:时序分析:隐马尔科夫模型 HMM用于手势识别: 训练时每一种手势对应一个HMM-Model,识别率取最大的一个HMM即可. 类似于一个封装的完成多类识别器功能单层网络. 优点: 尤其 ...
- 浅谈 Unserscore.js 中 _.throttle 和 _.debounce 的差异
来源:http://blog.coding.net/blog/the-difference-between-throttle-and-debounce-in-underscorejs Unsersco ...
- rev
功能说明:反向输出文件内容. 字符串反转 文本反转
- fiddler抓包 IOS11以上系统
想要使用fiddler对iphone X进行抓包,按照以前的配置方法,走一遍.还是抓不成,原来是因为IOS11操作系统中,安装完证书之后,还需要再手动信任证书才能正常抓包. 以下内容来自:https: ...
- CorelDRAW关于使用鼠标的5个技巧分享
CorelDRAW重度依赖者对快捷键和技巧性的操作爱不释手.本文我们介绍使用CorelDRAW关于鼠标操作的5个技巧,这是五种超快,非常有效,特别实用的技巧,如果每天的工作结合这些技巧的使用,效率会大 ...
- BZOJ2251 [2010Beijing Wc]外星联络 后缀数组 + Height数组
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in", "r", stdin ...
- eas源码示例1
EditUI: this.kdtEntrys.getRow(0).getCell(1).setValue("这个是第一行的第1个单元格的值"); public void loa ...
- 多重循环、缓冲区管理、数组(day06)
无法预知的数字叫随机数 rand标准函数可以用来获得随机数 为了使用这个标准函数需要包含stdlib.h头文件 srand标准函数用来设置随机数种子 这个函数把一个整数作为种子使用 不同的种子产生的随 ...
- Linux思维导图之进程管理
查漏补缺,理解概念,及时总结,欢迎拍砖.