HDU 1853 MCMF
题意:给定一个有向带权图,使得每一个点都在一个环上,而且权之和最小。
分析:每个点在一个环上,入度 = 出度 = 1,拆点入点,出点,s到所有入点全部满载的最小费用MCMF;
#include <bits/stdc++.h> using namespace std; const int maxn = *;
const int INF = 0x3f3f3f3f; typedef pair<int,int> pii; struct Edge
{
int from, to, cap, flow, cost;
}; struct MCMF
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn]; // 是否在队列中
int d[maxn]; // Bellman-Ford
int p[maxn]; // 上一条弧
int a[maxn]; // 可改进量 void init(int n)
{
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap, int cost)
{
edges.push_back((Edge)
{
from, to, cap, , cost
});
edges.push_back((Edge)
{
to, from, , , -cost
});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BellmanFord(int s, int t, int &flow, long long& cost)
{
memset(inq,,sizeof(inq));
for(int i=;i<n;i++)
d[i] = INF;
d[s] = ;
inq[s] = true;
p[s] = ;
a[s] = INF; queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
inq[u] = false;
for(int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to] = true;
}
}
}
}
if(d[t] == INF) return false; //s-t 不连通,失败退出
flow += a[t];
cost += (long long)d[t] * (long long)a[t];
int u = t;
while(u != s)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
u = edges[p[u]].from;
}
return true;
} pair<long long,int>Mincost(int s, int t)
{
long long cost = ;
int flow = ;
while(BellmanFord(s, t, flow, cost));
return pair<int,long long>{flow,cost};
}
}sol; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF) {
int s = ,t=*n+;
sol.init(*n+); for(int i=;i<=n;i++)
sol.AddEdge(s,i,,); for(int i=n+;i<=*n;i++)
sol.AddEdge(i,t,,); for(int i=;i<m;i++) {
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
sol.AddEdge(u,v+n,,c);
} pii ans = sol.Mincost(s,t);
if(ans.first!=n)
puts("-1");
else cout<<ans.second<<endl; }
return ;
}
HDU 1853 MCMF的更多相关文章
- HDU 1853
http://acm.hdu.edu.cn/showproblem.php?pid=1853 和下题一模一样,求一个图环的并,此题的题干说的非常之裸露 http://www.cnblogs.com/x ...
- HDU(1853),最小权匹配,KM
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...
- 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 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 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- 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
Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...
- hdu 1853(拆点判环+费用流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU 2686 MCMF
题意:两遍最长路,不能走重复点.和UVA 10806类似. 分析:拆点,u->u',MCMF,求的是最大流的最小费用,那么cost取负. 注意的是源点,源点不用拆,那么走出来的最小费用,左上角的 ...
随机推荐
- pandas中,dataframe 进行数据合并-pd.concat()
``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...
- python-URL转jpg图片
问题描述 有图片地址,可以在网页打开 URL:https://bdfile.bluemoon.com.cn/group2/M00/0A/BA/wKg_HlwzY1SAIdXDAAFyo-ZOLKQ39 ...
- nyoj 456——邮票分你一半——————【背包思想搜索】
邮票分你一半 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小珂最近收集了些邮票,他想把其中的一些给他的好朋友小明.每张邮票上都有分值,他们想把这些邮票分 ...
- BNU29376——沙漠之旅——————【技巧题】
沙漠之旅 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: M ...
- nginx安装及优化
1.pcre及nginx安装包下载 wget http://www.pcre.org/ pcre用yum安装即可 http://nginx.org/en/download.html 2.安装 -安 ...
- 2017年10月29日 数据库查询总结&45道题
日期函数: 当前时间:GetDate() 两个时间差:DateDiff() 一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Tea ...
- 序列化json和protobuf大小比较
使用protobuf序列化为二进制和json序列化字符串大小比较 代码demo package com.gxf.demo; import java.io.*; public class Ptotobu ...
- clearfix为什么用display:table,而不用display:block
我们都知道clearfix一般这么写: .clearfix:before,.clearfix:after{ content:""; display:table; } .clearf ...
- 分享一个好东西(一天精通MongoDB数据库)
https://pan.baidu.com/s/1o7V5e8U 总共几个小时的视频,看了之后醍醐灌顶.分享出来.
- (转)防止ViewPager中的Fragment被销毁的方法
在使用ViewPager与Fragment的时候,ViewPager会自动缓存1页内的数据,如下图: 当我们当前处在页面2的时候,页面1和页面3的View实际上已经创建好了,所以在我们拖动的时候是可以 ...