hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1197 Accepted Submission(s): 626
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).
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
-1
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
题意:
给一个图,要求用若干个环遍历全部点,求最短路程。
二分匹配KM最小权值做法:
//46MS 284K 2108 B C++
/*
二分匹配:
KM算法最小权值模板题...
*/
#include<stdio.h>
#include<string.h>
#define inf 0x7ffffff
int g[][];
int slack[];
int match[];
int lx[],ly[],visx[],visy[];
int n,m;
int Min(int a,int b)
{
return a<b?a:b;
}
int Max(int a,int b)
{
return a>b?a:b;
}
int dfs(int x)
{
visx[x]=;
for(int i=;i<=n;i++){
if(!visy[i]){
if(lx[x]+ly[i]==g[x][i]){
visy[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}else{
slack[i]=Min(slack[i],lx[x]+ly[i]-g[x][i]);
}
}
}
return ;
}
void KM()
{
memset(match,-,sizeof(match));
for(int i=;i<=n;i++) lx[i]=-inf;
memset(ly,,sizeof(ly));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
lx[i]=Max(lx[i],g[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
slack[j]=inf;
while(true){
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(dfs(i)) break;
int temp=inf;
for(int j=;j<=n;j++)
if(!visy[j])
temp=Min(temp,slack[j]);
for(int j=;j<=n;j++){
if(visx[j]) lx[j]-=temp;
if(visy[j]) ly[j]+=temp;
else slack[j]-=temp;
}
} }
}
int main(void)
{
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
g[i][j]=-inf;
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
if(-c>g[a][b])
g[a][b]=-c;
}
KM();
int ans=;
for(int i=;i<=n;i++){
if(match[i]==- || g[match[i]][i]==-inf){
ans=inf;
break;
}
ans+=g[match[i]][i];
}
if(ans==inf) puts("-1");
else printf("%d\n",-ans);
}
return ;
}
最小费用最大流做法:
//218MS 656K 2377 B C++
/* 第一题最小费用最大流...基本都是抄= =
注意边的初始化、添加和修改.. */
#include<iostream>
#include<queue>
#define inf 0x7ffffff
#define N 105
using namespace std;
struct node{
int u,v,c,w;
int next;
}edge[*N*N];
int n,m,edgenum,sumflow;
int head[*N],d[*N],pp[*N]; //pp记录增广链
bool vis[*N];
void init() //初始化
{
edgenum=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int c,int w) //添加双向边
{
edge[edgenum].u=u;
edge[edgenum].v=v;
edge[edgenum].c=c;
edge[edgenum].w=w;
edge[edgenum].next=head[u];
head[u]=edgenum++;
edge[edgenum].u=v;
edge[edgenum].v=u;
edge[edgenum].c=; //逆向边没流量
edge[edgenum].w=-w; //值取负
edge[edgenum].next=head[v];
head[v]=edgenum++;
}
bool spfa() //求最短路
{
queue<int>Q;
memset(vis,false,sizeof(vis));
memset(pp,-,sizeof(pp));
for(int i=;i<=*(n+);i++) d[i]=inf;
vis[]=true;
d[]=;
Q.push();
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=false;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(edge[i].c && d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
pp[v]=i;
if(!vis[v]){
Q.push(v);
vis[v]=true;
}
}
}
}
if(d[*n+]==inf) return false;
return true;
}
int MCMF()
{
int t=*n+;
int flow=;
int mincost=;
sumflow=;
while(spfa()){
int minflow=inf+;
for(int i=pp[t];i!=-;i=pp[edge[i].u])
if(edge[i].c<minflow)
minflow=edge[i].c;
flow+=minflow;
for(int i=pp[t];i!=-;i=pp[edge[i].u]){ //调整增广链
edge[i].c-=minflow;
edge[i^].c+=minflow; //逆向边
}
mincost+=d[t]*minflow;
}
sumflow=flow;
return mincost;
}
int main(void)
{
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=;i<=n;i++){
addedge(,i,,);
addedge(n+i,*n+,,);
}
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b+n,,c);
}
int ans=MCMF();
if(sumflow!=n) puts("-1");
else printf("%d\n",ans);
}
return ;
}
hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)的更多相关文章
- hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- hdu 1853 Cyclic Tour 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...
- HDU 1853 Cyclic Tour[有向环最小权值覆盖]
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU 1853 Cyclic Tour(最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- hdu1853 Cyclic Tour (二分图匹配KM)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- 【刷题】HDU 1853 Cyclic Tour
Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour
题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...
- poj3565 Ants km算法求最小权完美匹配,浮点权值
/** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...
随机推荐
- 使用docker安装和运行常用的数据库和中间件
mysql: docker pull mysql: docker run --name mysql -p : -v /usr/share/zoneinfo/Asia/Shanghai:/etc/loc ...
- C#正则表达式Regex类的使用
C#中为正则表达式的使用提供了非常强大的功能,这就是Regex类.这个包包含于System.Text.RegularExpressions命名空间下面,而这个命名空间所在DLL基本上在所有的项目模板中 ...
- php中处理中文的注意
使用session的情况下------------------------- php.ini register_globals = Off 保持关闭,开启可能会导致iconv转换中文产生错误 修改ph ...
- Leecode刷题之旅-C语言/python-69x的平方根
/* * @lc app=leetcode.cn id=69 lang=c * * [69] x 的平方根 * * https://leetcode-cn.com/problems/sqrtx/des ...
- POJ2186 强连通分量+缩点
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40234 Accepted: 16388 De ...
- C# 生成机器码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- JAVA 基础编程练习题
1 [程序 1 不死神兔] 题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?程序分析: 兔子的规 ...
- Python3 函数return
# def logger(): # f = open("loge.txt","a") # f.write("2017-09-15 exec funct ...
- YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)
题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...