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取负. 注意的是源点,源点不用拆,那么走出来的最小费用,左上角的 ...
随机推荐
- 关于工具类静态方法调用@Autowired注入的service类问题
@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...
- stream3
import java.util.function.Consumer; import java.util.function.IntConsumer; public class ConsumerTest ...
- linux 命令之重定向
linux 重定向及部分命令 一,重定向讲解: 1> 标准输出重定向 覆盖原有内容 慎用!!!!!! 1>> 标准输出追加重定向 追加内容 2> 错误输出重定向 只输出错误信息 ...
- (转)PEP 8——Python编码风格指南
PEP 8——Python编码风格指南标签(空格分隔): Python PEP8 编码规范原文:https://lizhe2004.gitbooks.io/code-style-guideline-c ...
- TimesTen启动停止命令
ttDaemonAdmin –start 启动 ttDaemonAdmin –stop 停止 或打开服务cmd-serviers.msc,找到相关服务启动或停止.
- TOJ 2926 Series
Description An arithmetic series consists of a sequence of terms such that each term minus its immed ...
- Unity3D游戏轻量级xlua热修复框架
Unity3D游戏轻量级xlua热修复框架 一 这是什么东西 前阵子刚刚集成xlua到项目,目的只有一个:对线上游戏C#逻辑有Bug的地方执行修复,通过考察xlua和tolua,最终选择了xlua ...
- Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)
Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性) 前言:由于前段时间忙于写接口,在接口中需要做很多的参数校验,本着简洁.高效的原则,便写了这个小工具供自己使用(内容 ...
- 简单的Extjs中的Combox选择下拉框使用
{ xtype: "combobox", editable: false, emptyText: "--请选择--", mode: 'local', store ...
- SpringSecurity 3.2入门(9)自定义权限控制代码实现
1. 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,我们的所有控制将在 ...