POJ2135

最小费用最大流模板题

题意就是要找两条无交集的从1到n的路线,使距离和最小。

每条路只能走一次。

设置一个源,接到1点,设置一个汇,从n点接到汇。

为保证无交集,我们把每条边的流量设置为1,而源发出的流量和汇接收的流量均为2。每条边的费用就是该边在原图中的权值。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <queue>
 5 using namespace std;
 6 const int N=1050;
 7 const int M=10050;
 8 struct{
 9     int v, cap, cost, next, re;//re 逆边的下标
10 }e[M];
11 int n,m,ans,k;
12 int first[N],pre[N],dis[N];
13 void init(){
14     k=0;
15     memset(first,-1,sizeof(first));
16     ans=0;
17 }
18 void addedge(int u,int v,int ca,int co){
19     e[k].v=v;
20     e[k].cap=ca;
21     e[k].cost=co;
22     e[k].next=first[u];
23     e[k].re=k+1;
24     first[u]=k++;
25     e[k].v=u;
26     e[k].cap=0;
27     e[k].cost=-co;
28     e[k].next=first[v];
29     e[k].re=k-1;
30     first[v]=k++;
31 }
32 bool spfa(int s,int t){
33     memset(dis,0x3f,sizeof(dis));
34     queue<int >q;
35     int inq[N]={0};
36     q.push(s);
37     inq[s]=1;
38     dis[0]=0;
39     while(!q.empty()){
40         int u=q.front();
41         q.pop();
42         for(int i=first[u];i!=-1;i=e[i].next){
43             int v=e[i].v;
44             if(e[i].cap&&dis[v]>dis[u]+e[i].cost){
45
46                 dis[v]=dis[u]+e[i].cost;
47                 pre[v]=i;
48                 if(!inq[v]){
49                     inq[v]=1;
50                     q.push(v);
51                 }
52             }
53         }
54         inq[u]=0;
55     }
56     if(dis[t]==0x3f3f3f3f){return false;}
57     return true;
58 }
59 void solve(){
60     int u, p, sum = 1<<30;
61     for(u = n; u != 0; u = e[e[p].re].v){
62         p = pre[u];
63         sum = min(sum, e[p].cap);
64     }
65     for(u = n; u != 0; u = e[e[p].re].v){
66         p = pre[u];
67         e[p].cap -= sum;
68         e[e[p].re].cap += sum;
69         ans += sum * e[p].cost;
70     }
71 }
72 int main(){
73     int u,v,c;
74     init();
75     scanf("%d%d",&n,&m);
76     while(m--){
77         scanf("%d%d%d",&u,&v,&c);
78         addedge(u,v,1,c);
79         addedge(v,u,1,c);
80     }
81     addedge(0,1,2,0);
82     addedge(n,n+1,2,0);
83
84     n++;
85     while(spfa(0,n)){solve();}
86     printf("%d\n",ans);
87     return 0;
88 }
时间: 2024-10-11 00:06:15

POJ2135的相关文章

[POJ2135]最小费用最大流

一直由于某些原因耽搁着...最小费用最大流没有搞会. 今天趁着个人状态正佳,赶紧去看看,果然30min不到看会了算法+模板并且A掉了一道题. 感觉最小费用最大流在学过了最大流之后还是挺好理解的.找到从起点到终点流过1单位流量的最小花费方案,然后更新数据. 不停地找增广路,不停累计答案,不停趋近最优解. 理解起来没有任何问题.代码书写一遍就过了很顺利. POJ2135 实际上是一道并不那么容易套的模板题. 网络流的题目重在建模.这道题就是这样. 求起点到终点往返的最短路径,但不能经过相同的边. 往

poj2135(简单的最小费用流问题)

题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10862   Accepted: 4024 Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000

POJ2135 最小费用最大流模板题

练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include <cstring> #define MAXN 1005 #define MAXM 10005 #define INF 0x3f3f3f3f struct Edge { int y,c,w,ne;//c容量 w费用 }e[MAXM*4]; int n,m,x,y,w; int s,t,Maxflow

no-understand 最小费用最大流-poj-2135

Farm Tour Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 &

解题报告 之 POJ2135 Farm Tour

解题报告 之 POJ2135 Farm Tour Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn.

poj2135 最小费用流

添加超级源点(与点1之间的边容量为2,权值为0)和超级汇点(与点N之间的边容量为2,权值为0),求流量为2的最小费用流.注意是双向边. #include <iostream> #include <cstdio> #include <vector> #include <queue> using namespace std; const long long INF = 0x3f3f3f3f3f3f3f3f; typedef long long ll; typed

POJ2135:Farm Tour

http://poj.org/problem?id=2135 #include <iostream> #include <queue> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <stdio.h> #define inf 0x3f3f3f3f using namespace std; int

poj2135最小费用最大流经典模板题

Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13509   Accepted: 5125 Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of

POJ2135 Farm Tour 【最小费用最大流】

Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11782   Accepted: 4393 Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of