题意:给定一个有向带权图,使得每一个点都在一个环上,而且权之和最小。

分析:每个点在一个环上,入度 = 出度 = 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的更多相关文章

  1. HDU 1853

    http://acm.hdu.edu.cn/showproblem.php?pid=1853 和下题一模一样,求一个图环的并,此题的题干说的非常之裸露 http://www.cnblogs.com/x ...

  2. HDU(1853),最小权匹配,KM

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

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

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

  4. hdu 1853 最小费用流好题 环的问题

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

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

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

  6. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

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

  7. 【刷题】HDU 1853 Cyclic Tour

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

  8. hdu 1853(拆点判环+费用流)

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

  9. HDU 2686 MCMF

    题意:两遍最长路,不能走重复点.和UVA 10806类似. 分析:拆点,u->u',MCMF,求的是最大流的最小费用,那么cost取负. 注意的是源点,源点不用拆,那么走出来的最小费用,左上角的 ...

随机推荐

  1. Linux批量杀掉挂掉的进程

    $ `ps aux | grep test | grep -v grep | awk '{print $2}'` 杀掉含有test且不含有grep的进程,后面的 awk '{print $2}' 是进 ...

  2. Epplus导出Excel(DataTable)

    1.先将dataTable转换成流 public Stream DataTableToExcel(DataTable dataTable, string[] columns, string sheet ...

  3. 案例47-crm练习登录校验拦截器

    1 LoginInterceptor package www.test.web.interceptor; import java.util.Map; import com.opensymphony.x ...

  4. Android RelativeLayout 属性 转自互联网

    // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below 将该控件的底部置于给定ID的控件之下; andro ...

  5. 给python解释器本身添加注册表

    import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.pr ...

  6. Basic Data Structures and Algorithms in the Linux Kernel--reference

    http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...

  7. 用jquery来实现正反选选择框checkbox的小示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JS常用的设计模式(2)——简单工厂模式

    简单工厂模式是由一个方法来决定到底要创建哪个类的实例, 而这些实例经常都拥有相同的接口. 这种模式主要用在所实例化的类型在编译期并不能确定, 而是在执行期决定的情况. 说的通俗点,就像公司茶水间的饮料 ...

  9. jQuery前端数据通用验证库,解放你的双手

    这个简易的验证库,应该能完成90%的基本验证,包括失去焦点时的验证,以及点击提交按钮时的验证.后端的那我就无能为办了,只能是谁用就谁自个儿去写了:). 先上一段调用的代码吧,JS代码说少也不少了,就不 ...

  10. SQL Server Profiler(转载)

    SQL Server Profiler工具 一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收 ...