Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1688    Accepted Submission(s): 859

Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
 
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
 
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1. 
 
Sample Input
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
6 5
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
 
Sample Output
42
-1

Hint

In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

 
题意:给一个有向有权图,选一些边使得每个点都在且只在一个环之中,且要求权值走过的边的权值最小
 
思路:

    可以发现,每个点的入度和出度都是1。

如果每个点都拆成入点和出点,对于点u,可以拆成u和u’, u是入点,u’是出点。

若有边(u, v),则u’ -> v连边

这样整个图转化为一个二分图。由于每个入点需要找一个出点连接,每个出点也要找一个入点连接,那么就是经典的二分图匹配问题。加上一个权值,就是二分图最优匹配问题。用KM或者最小费用流都可以解决。

这题卡了几天,因为KM模版都是每一个x点都和每一个y点连上了,而这条题目只是部分连了而已,不懂得处理

后来看题解处理方法只是最后统计答案的时候,如果g[linker[y]][y]==-INF的话就跳过

求最小匹配的处理就是把权值换成负的就可以了

第一次交wa了,因为没考虑到重边的情况,这些题要考虑判重自环呀

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <utility>
#include <queue>
#include <stack>
using namespace std;
const int INF=1e9;
const double eps=1e-;
const int N = ; int nx,ny;
int g[N][N];
int linker[N],lx[N],ly[N];// x is outpoint, y is inpoint
int slack[N];
int visx[N],visy[N]; int n,m; bool DFS(int x)
{
visx[x]=true;
for(int y=;y<ny;y++)
{
if(visy[y]) continue;
int tmp = lx[x]+ly[y]-g[x][y];
if(tmp==)
{
visy[y]=true;
if(linker[y]==-||DFS(linker[y]))
{
linker[y]=x;
return true;
}
}
else if(slack[y]>tmp)
slack[y]=tmp;
}
return false;
} int KM()
{
memset(linker,-,sizeof(linker));
memset(ly,,sizeof(ly));
for(int i=;i<nx;i++)
{
lx[i]=-INF;
for(int j=;j<ny;j++)
if(g[i][j]>lx[i])
lx[i]=g[i][j];
}
for(int x=;x<nx;x++)
{
for(int i=;i<ny;i++)
slack[i]=INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x)) break;
int d = INF;
for(int i=;i<ny;i++)
if(!visy[i] && d>slack[i])
d=slack[i];
for(int i=;i<nx;i++)
if(visx[i])
lx[i]-=d;
for(int i=;i<ny;i++)
{
if(visy[i]) ly[i]+=d;
else slack[i]-=d;
}
}
}
int res = , cnt = ;
for(int i=;i<ny;i++)
{
if(linker[i]==- || g[linker[i]][i]==-INF)
continue;
res += g[linker[i]][i];
cnt++;
}
if(cnt!=nx) return -;
return -res;
} void run()
{
// memset(g,0,sizeof(g));
for(int i=;i<n;i++)
for(int j=;j<n;j++)
g[i][j]=-INF;
int u,v,c;
while(m--)
{
scanf("%d%d",&u,&v);
scanf("%d",&c);
if(-c>g[u-][v-])
g[u-][v-]=-c;
}
nx=ny=n;
printf("%d\n",KM());
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif // LOCAL
while(scanf("%d%d",&n,&m)!=EOF)
run();
return ;
}

hdu1853 Cyclic Tour (二分图匹配KM)的更多相关文章

  1. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

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

  2. hdu1853 Cyclic Tour 完美匹配 验证模版

    题意: 给出n个城市和m条路,每个城市只能经过一次,想要旅游所有的城市,求需要的最小花费(路径的长度). 分析: 做题之前,首先要知道什么是完美匹配.不然题目做了却不知道为什么可以用这个方法来做.完美 ...

  3. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  4. HDU1853 Cyclic Tour

    Cyclic Tour                                                                                Time Limi ...

  5. 牛客多校第五场 E room 二分图匹配 KM算法模板

    链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...

  6. 模板——二分图匹配KM

    具体方法就不介绍了,详见 https://blog.csdn.net/sixdaycoder/article/details/47720471 主要讲一些注意点: 1:不直接将未匹配的y减小是因为要保 ...

  7. Assignment HDU - 2853(二分图匹配 KM 新边旧边)

    传送门: Assignment HDU - 2853 题意:题意直接那松神的题意了.给了你n个公司和m个任务,然后给你了每个公司处理每个任务的效率.然后他已经给你了每个公司的分配方案,让你求出最多能增 ...

  8. 二分图匹配--KM算法

    Kuhn-Munkres算法 KM算法,求完备匹配下的最大权匹配,时间复杂度O(\(n^3\)) 所谓的完备匹配就是在二部图中,x点集中的所有点都有对应的匹配 且 y点集中所有的点都有对应的匹配 ,则 ...

  9. HDU-1853 Cyclic Tour

    最小权值环覆盖问题:用几个环把所有点覆盖,求所选取的边最小的权值之和. 拆点思想+求最小转求最大+KM算法 #include <cstdlib> #include <cstdio&g ...

随机推荐

  1. 【BZOJ2654】tree 二分+最小生成树

    [BZOJ2654]tree Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need ...

  2. 九度OJ 1061:成绩排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17158 解决:4798 题目描述: 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相 ...

  3. mybatis 运算符转义收录

    在ibatis配置文件写SQL语句的时候对于一些比如“<”,">","<>","&"," ' &q ...

  4. 列举Python常用数据类型并尽量多的写出其中的方法

    #1 把字符串的第一个字符大写 string.capitalize() #2 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 string.center(width) #3 返回 ...

  5. 爬虫-Selenium -抱错ElementNotVisibleException: Message: element not visible

    1.当使用Selenium IDE 完成了脚本的录制和回放通过后,想要将脚本转换为其他语言如java.Python等,首次使用时打开Options->Format发现没有可以转换的语言,如下: ...

  6. B. Two Buttons

    这是Codeforces Round #295 (Div. 2) 的B 题,题意为: 给出n, m, 有两种操作,n 减一 和 n 乘以 2,问最少要多少次操作才能把n 变成 m. Sample te ...

  7. 理解Java泛型 通配符 ? 以及其使用

    什么是泛型: 泛型从字面上理解,是指一个类.接口或方法支持多种类型,使之广泛化.一般化和更加通用.Java中使用Object类来定义类型也 能实现泛型,但缺点是造成原类型信息的丢失,在使用中容易造成C ...

  8. 怎么样写一个能告诉你npm包名字是否被占用的工具

    事情是这样的: 因为我经常会写一些npm包,但是有时候我写完一个包,npm publish 的时候却被提示说包名字被占用了,要不就改名字,要不就加scope,很无奈.npm 命令行可以通过 npm v ...

  9. CSS3 3D下拉折叠菜单

    在线演示 本地下载

  10. ActiveMQ 了解

    Active MQ 是JMS的一个具体实现,所以首先要对JMS有所了解. 1.       JMS: 定义:Java消息服务(Java Message Service),是Sun是提出来的为J2EE提 ...