Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 2549    Accepted Submission(s): 1257
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.
 
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.
 
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   |   We have carefully selected several similar problems for you:  3435 1853 3395 3491 3657 

#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)的更多相关文章

  1. HDU - 3488 Tour (KM最优匹配)

    题意:对一个带权有向图,将所有点纳入一个或多个环中,且每个点只出现一次,求其所有环的路径之和最小值. 分析:每个点都只出现一次,那么换个思路想,每个点入度出度都为1.将一个点拆成两个点,一个作为入度点 ...

  2. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  3. hdoj 3488 Tour 【最小费用最大流】【KM算法】

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  4. HDU 3488 Tour(最小费用流:有向环最小权值覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...

  5. HDU3488:Tour(KM算法)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. 【UVA 1411】 Ants (KM)

    Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...

  7. 【HDU 2853】Assignment (KM)

    Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...

  8. 奔小康赚大钱(km)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. 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 ...

随机推荐

  1. 关于如何将_variant_t型转化为int型和string类型

    1)将_variant_t型转化为int型 关于将_variant_t型转化为int型,网上有好多好多参考,但好多都很复杂并且还不对,其实整个转化过程就只一行代码可以搞定: _variant_t a; ...

  2. SQLServer外部数据导入--Excel版

    例如要在test表里插入多行数据 假设字段有: ID.Name 首先要有需要导入的数据的Excel A1 对应ID B1 对应Name 选中Excel第一行的空白处,比如C1,在工具栏的函数文本框里输 ...

  3. 《java数据结构与算法》系列之“开篇”

    大学的时候学习数据结构,当时吧虽然没挂这门课,但是确实学的不咋地,再但是其实自己一直都觉得数据结构很重要,是基础,只有基础好了,后面的路才能走的更好. 懒惰真的是天下的罪恶之源.所以一直到现在都毕业了 ...

  4. sql server like 在将值转换成数据类型int失败

    select * from table where title like '%'?'%'; 采用? 传参会报错:sql server like 在将值转换成数据类型int失败 select * fro ...

  5. 基于 CC2530 的温度采集系统(未定稿)

    前言 最近在自学 Zigbee,每天的主要是任务是:看博客,看 CC2530 的 datasheet 和实践,熟悉片上的 SFR 以及控制板子. 学和做内容包括:IO.外部中断.Timer1/3/4. ...

  6. dubbo之直连提供者

    在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候可能需要点对点直连,点对点直联方式,将以服务接口为单位,忽略注册中心的提供者列表,A 接口配置点对点,不影响 B 接口从注册中心获 ...

  7. JavaOO知识点小结一

    Java语言的特点是什么?简单 面向对象 跨平台 多线程 健壮性安全性 垃圾回收机制如何编译和执行java文件?产生帮助文档用什么命令?编译: javac 文件名执行: java 类名产生帮助文档 j ...

  8. CNN结构:用于检测的CNN结构进化-结合式方法

    原文链接:何恺明团队提出 Focal Loss,目标检测精度高达39.1AP,打破现有记录     呀 加入Facebook的何凯明继续优化检测CNN网络,arXiv 上发现了何恺明所在 FAIR 团 ...

  9. IronPython中共享的C#基类如何向下转型

    在项目中,我们使用IronPython来定义工作流脚本来以应对科研多变的需求.项目使用的主要语言仍然是C#,使用C#封装了各种基础服务与基础设施.Python脚本只使用C#提供的服务,或者说只定义了逻 ...

  10. layer自定义弹窗样式

    1.下载并引用js, 官网http://layer.layui.com/ 文档http://www.layui.com/doc/modules/layer.html <link href=&qu ...