hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1904 Accepted Submission(s): 951
all the tours minimum, but he is too lazy to calculate. Can you help him?
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).
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
42
-1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
题意:
给你若干个点和带权有向边,要求把全部点连成环。能够多个环。可是每一个环至少要有两个点。
做法:
全部的点成环,能够知道全部的点 入度和出度都为1。而且仅仅要符合这个条件,全部点肯定是在一个环中的,也就是符合条件了。
所以能够建一个二分图,左边的点从s流入费用为0,流量为1。表示入度为1 ,右边一样。
然后依据边 建流量为1,费用为边权的边,这就是最大权值匹配的图了。
这样仅仅要满流就符合条件了。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
//最小费用最大流。求最大费用仅仅须要取相反数,结果取相反数就可以。
//点的总数为 N,点的编号 0~N-1
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数。节点编号从0~N-1
void init(int n)
{
N = n;
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i = 0;i < N;i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost )
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -1)return false;
else return true;
}
//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
int flow = 0;
cost = 0;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
{
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
init(2*n+2);
int ss=0;
int ee=2*n+1;
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w); addedge(u,v+n,1,w); }
for(int i=1;i<=n;i++)
{
addedge(ss,i,1,0);
addedge(i+n,ee,1,0);
} int cost,liu;
liu=minCostMaxflow(ss,ee,cost);
if(liu!=n)
{
puts("-1");
}
else
{
printf("%d\n",cost); } } return 0;
}
hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和的更多相关文章
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
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)Total ...
- HDU 1853 Cyclic Tour(最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- 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
Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...
- Tour HDU - 3488(最大权值匹配)
Tour In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one- ...
- 奔小康赚大钱 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 ...
- HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
随机推荐
- 使用Spring Security和OAuth2实现RESTful服务安全认证
这篇教程是展示如何设置一个OAuth2服务来保护REST资源. 源代码下载github. (https://github.com/iainporter/oauth2-provider)你能下载这个源码 ...
- 【小程序】wxs使用
wxs使用 WXS(WeiXin Script)是小程序的一套脚本语言,结合WXML,可以构建出页面的结构. wxs可以说就是为了满足能在页面中使用js存在的,在wxml页面中,只能在插值{{ }}中 ...
- ntpdate设置
ntpdate设置 学习了:https://www.cnblogs.com/ibnode/p/3573302.html http://www.blogjava.net/spray/archive/20 ...
- MFC apps must not include windows.h
用VS2008建立一个DLL项目,一开始的时候不想用MFC, 所以选择的是使用标准Windows库. 使用了一段时间后又想用MFC了,所以把选项改成使用在共享 DLL 中使用 MFC. 但是编译的时候 ...
- 如何安装Tomcat
1 请确认已经安装了JRE或JDK并配置好了环境变量,关于如何配置环境变量,参考我的另一篇文章"WIN7如何配置java环境变量,运行环境.doc" 2 用记事本打开bin目录下的 ...
- iOS \U7ea2 乱码 转换
通常网络请求的数据,如果不做处理在输出时显示是 \U 之类的编码的: 不需要导入别的类库解决方法 - (NSString *)replaceUnicode:(NSString *)unicodeStr ...
- hadoop safemode error
http://www.cnblogs.com/hustcat/archive/2010/06/30/1768506.html 1.safemode bin/hadoop fs -put ./input ...
- HDU 2883 kebab(最大流)
HDU 2883 kebab 题目链接 题意:有一个烧烤机,每次最多能烤 m 块肉.如今有 n 个人来买烤肉,每一个人到达时间为 si.离开时间为 ei,点的烤肉数量为 ci,每一个烤肉所需烘烤时间为 ...
- Android中保存静态秘钥实践(转)
本文我们将讲解一个Android产品研发中可能会碰到的一个问题:如何在App中保存静态秘钥以及保证其安全性.许多的移动app需要在app端保存一些静态字符串常量,其可能是静态秘钥.第三方appId等. ...
- [Done]mysql in (#{list}) 只能查询/删除第一条的问题
数据如下(注意age是int类型): sql如下(注意是#不是$): java代码: Mybatis日志(只返回一笔记录): 直接在mysql中执行(age是int类型,注意参数带引号,确认jdbc是 ...