NWERC 2012 A - Admiral【最小费用最大流】

题意:

有n个岛屿 起点1 终点 n

现在两个人同时从起点出发向终点前进

两个人的路径不能有交集(一个人走过的路另一个人不能再走了)

问两个人的最小总花费

分析:

刚开始的的时候以为是最短路和次短路的和就行了   (太天真了)

快要结束的时候突然间发现这不就是传说中的最小费用最大流问题吗

可惜时间不够

思路是这样的:因为两个人来走,所以输入和输出的流量为1 费用为0

而连接的两点流量为1花费为权值

建图即可

结束之后敲完了代码,测试数据通过

不过没有地方提交

在程的帮助下重启pc^2完成了提交,

可是

结果却是wrong

很苦恼啊

于是各种修改啊

还是wrong

最后在网上搜了一下题解

我终于知道wrong的地方了

就跟牛选食物跟饮料那个题一样的 ,如果一头牛同时喜欢两种食物和两种饮料的话,结果就是不对的

这个题类似

修改方法便是拆点

代码如下:

  1 //已ac
2 #include <iostream>
3 #include <cstdio>
4 #include <cstring>
5 #include <vector>
6 #include <queue>
7 using namespace std;
8
9 const int maxn = 10005;
10 const int INF = 1000000000;
11
12 struct Edge {
13 int from, to, cap, flow, cost;//起点 终点 容量 花费
14 };
15
16 struct MCMF
17 {
18 int n, m, s, t;
19 vector<Edge> edges;
20 vector<int> G[maxn];
21
22 int inq[maxn];
23 int d[maxn];
24 int p[maxn];
25 int a[maxn];
26
27 void init(int n) {//初始化的是所有的点数
28 this -> n = n;
29 for(int i = 0; i < n; i++) G[i].clear();
30 edges.clear();
31 }
32
33 void AddEdge(int from, int to, int cap, int cost) {
34 edges.push_back((Edge) { from, to, cap, 0, cost } );
35 edges.push_back((Edge) { to, from, 0, 0, -cost } );
36 m = edges.size();
37 G[from].push_back(m - 2);
38 G[to].push_back(m - 1);
39 }
40
41 bool BellmanFord(int s, int t, int &flow, int &cost) {
42 for(int i = 0; i < n; i++) d[i] = INF;
43 memset(inq, 0, sizeof(inq) );
44 d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
45 queue<int> Q;
46 Q.push(s);
47 while(!Q.empty()) {
48 int u = Q.front(); Q.pop();
49 inq[u] = 0;
50 for(int i = 0; i < G[u].size(); i++) {
51 Edge &e = edges[G[u][i]];
52 if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
53 d[e.to] = d[u] + e.cost;
54 p[e.to] = G[u][i];
55 a[e.to] = min(a[u], e.cap - e.flow);
56 if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
57 }
58 }
59 }
60
61 if(d[t] == INF) return false;
62 flow += a[t];
63 cost += d[t] * a[t];
64 int u = t;
65 while(u != s) {
66 edges[p[u]].flow += a[t];
67 edges[p[u] ^ 1].flow -= a[t];
68 u = edges[p[u]].from;
69 }
70 return true;
71 }
72
73 int MinCost(int s, int t) {//起点和终点
74 // this -> s = s; this -> t = t;
75 int flow = 0, cost = 0;
76 while(BellmanFord(s, t, flow, cost)){};
77 return cost;
78 }
79 };
80
81 MCMF g;
82
83 int main() {
84 int n, m;
85 int a, b, c;
86 freopen("a.txt","r",stdin);
87 while(EOF != scanf("%d %d",&n, &m)) {
88 g.init(n * 2 + 1);
89 g.AddEdge(1, 1 + n, 2, 0);
90 g.AddEdge(n, n + n, 2, 0);
91 for(int i = 2; i <= n - 1; i++) {
92 g.AddEdge(i, i + n, 1, 0);
93 }
94 for(int i = 0; i < m; i ++) {
95 scanf("%d %d %d",&a, &b, &c);
96 g.AddEdge(a + n, b, 1, c);
97 }
98 printf("%d\n",g.MinCost(1, n + n));
99 }
100 return 0;
101 }

时间: 2024-08-04 10:16:46

NWERC 2012 A - Admiral【最小费用最大流】的相关文章

UVa 1658 Admiral(最小费用最大流)

拆点费用流 --------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<vector> #define rep(i,n) for(int i=0;i<n;

UVA1658 Admiral 拆点法解决结点容量(路径不能有公共点,容量为1的时候) 最小费用最大流

/** 题目:UVA1658 Admiral 链接:https://vjudge.net/problem/UVA-1658 题意:lrj入门经典P375 求从s到t的两条不相交(除了s和t外,没有公共点)的路径,使得权值和最小. 思路:拆点法. 除了s,t外.把其他点都拆成两个. 例如点A,拆成A和A'.A指向A'连一条容量为1,花费为0的边. 原来指向A的,仍然指向A点. 原来A指向其他点的,由A'指向它们. 最小费用最大流求流量为2时候的最小费用即可. */ #include<iostrea

最小费用最大流 POJ2195-Going Home

网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6732762 大致题意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man.现在要求所有的man都入住hou

【BZOJ3876】【Ahoi2014】支线剧情 有下界的最小费用最大流

#include <stdio.h> int main() { puts("转载请注明出处谢谢"); puts("http://blog.csdn.net/vmurder/article/details/43025375"); } [BZOJ2324]营救皮卡丘 这道题也是一道有下界的最小费用最大流. 我的题解地址:http://blog.csdn.net/vmurder/article/details/41378979 这道题其实就是模板题. 我的处理

POJ 3686.The Windy&#39;s 最小费用最大流

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5477   Accepted: 2285 Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The ma

P3381 【模板】最小费用最大流

P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行包含四个正整数ui.vi.wi.fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi. 输出格式: 一行,包含两个整数,依次为最大流量和在最大流量情况下的

C++之路进阶——最小费用最大流(支线剧情)

F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在Linux平台下,而许多题的数据在Windows下制作,请注意输入.输出语句及数据类型及范围,避免无谓的RE出现. 3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 542  Solved: 332[Submit

hdu 4494 Teamwork 最小费用最大流

Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 Description Some locations in city A has been destroyed in the fierce battle. So the government decides to send some workers to repair these location

POJ - 2195 Going Home(最小费用最大流)

1.N*M的矩阵中,有k个人和k个房子,每个人分别进入一个房子中,求所有人移动的最小距离. 2.人看成源点,房子看成汇点,求最小费用最大流. 建图-- 人指向房子,容量为1,费用为人到房子的曼哈顿距离. 建立超级源点和超级汇点:超级源点指向人,容量为1,费用为0:超级汇点指向房子,容量为1,费用为0. 求超级源点到超级汇点的最小费用最大流即可. ps:容量为什么都设为1?---有待研究.. 3. 1.Bellman-Ford: #include<iostream> #include<st

hdu 1853 Cyclic Tour 最小费用最大流

题意:一个有向图,现在问将图中的每一个点都划分到一个环中的最少代价(边权和). 思路:拆点,建二分图,跑最小费用最大流即可.若最大流为n,则说明是最大匹配为n,所有点都参与,每个点的入度和出度又是1,所以就是环. /********************************************************* file name: hdu1853.cpp author : kereo create time: 2015年02月16日 星期一 17时38分51秒 *******