Cyclic Tour

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

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.

 
Author
RoBa@TJU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1533 3395 3315 2448 3061 
 

题意:

给一个图,要求用若干个环遍历全部点,求最短路程。

二分匹配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最小权值 或 最小费用最大流)的更多相关文章

  1. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

  2. hdu 1853 Cyclic Tour 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...

  3. HDU 1853 Cyclic Tour[有向环最小权值覆盖]

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

  4. HDU 1853 Cyclic Tour(最小费用最大流)

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

  5. hdu1853 Cyclic Tour (二分图匹配KM)

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

  6. 【刷题】HDU 1853 Cyclic Tour

    Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...

  7. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

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

  8. 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour

    题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...

  9. poj3565 Ants km算法求最小权完美匹配,浮点权值

    /** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...

随机推荐

  1. shell 输出带颜色字体

    输出特效格式控制:\033[0m  关闭所有属性  \033[1m   设置高亮度  \03[4m   下划线  \033[5m   闪烁  \033[7m   反显  \033[8m   消隐  \ ...

  2. Spring笔记1

    Spring Spring特点 1. 方便解耦,简化开发 通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.有了Spring,用户 ...

  3. jenkins+maven+docker集成java发布(一)自动发布

    JAVA项目持续集成发布 标签(空格分隔): java jenkins 微服务中持续集成自动发布是很重要的一个环节,将不同的模块应用自动部署到一台或者N台服务器中如果采用人工部署的方式不太现实 git ...

  4. R语言学习笔记(十七):data.table包中melt与dcast函数的使用

    melt函数可以将宽数据转化为长数据 dcast函数可以将长数据转化为宽数据 > DT = fread("melt_default.csv") > DT family_ ...

  5. ccpc 2018 final G - Pastoral Life in Stardew Valley

    #include <iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  6. SAN---第二网的概念

    网络技术的优缺点:优点:连接能力,超强路由,管理能力,远距离缺点:低速以及高负载,强烈的软件需求,错误检测能力 SAN:storage area network(存储区域网络)--是一种基于光网的特殊 ...

  7. 【Consul】多数据中心

    Consul的一个关键特性是支持多数据中心.consul架构中提到是构建低耦合的多个数据中心,一个数据中心的网络连接问题或故障不在其他数据中心的可用性.每个数据中心都是独立运行,并且拥有私有的LAN ...

  8. ORB-SLAM(五)KeyFrame类

    KeyFrame类利用Frame类来构造.对于什么样的Frame可以认为是关键帧以及何时需要加入关键帧,是实现在tracking模块中的. 由于KeyFrame中一部分数据会被多个线程访问修改,因此需 ...

  9. dubbo心跳机制 (1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. dubbo的心跳机制: 目的:检测provider与consumer之间的connection连接是不是还连 ...

  10. Unity3d脚本生命周期

    如图: 测试脚本: using UnityEngine; public class Test2 : MonoBehaviour { void Awake() { Debug.Log("Awa ...