当前位置:网站首页>Minimum cost and maximum flow (template question)
Minimum cost and maximum flow (template question)
2022-06-24 21:10:00 【GS_ Qiang】
poj2125
The main idea of the topic :
Here's an undirected graph , Yes n A little bit ,m side , Seek from 1 ~ n, And then from n ~ 1 , And each side can only go once , Find the shortest path .
Ideas :
Because each side can only go once , So you can set the flow of each side to 1, Or flow through the edge , Or neither . Set the weight of each edge to the cost of that edge , Then run twice spfa The shortest way is to find the minimum cost .
Templates :
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1e3+10;
const int M=4e4+10;
int n,m,ss,tt;
int dis[N],head[M<<1],tot=1,vis[N],incf[N],pre[N];
struct edge {
int to,next,w,flow;
}E[M<<1];
void add(int from,int to,int flow,int w) {
E[++tot].to=to;
E[tot].flow=flow;
E[tot].w=w;
E[tot].next=head[from];
head[from]=tot;
}
bool spfa(int s,int t) {
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
q.push(s);
dis[s]=0,vis[s]=1;
incf[s]=INF; // Infinite initial flow
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];~i;i=E[i].next) {
if(!E[i].flow) continue;
int to=E[i].to;
if(dis[to]>dis[u]+E[i].w) {
dis[to]=dis[u]+E[i].w;
incf[to]=min(incf[u],E[i].flow);
pre[to]=i;
if(!vis[to]) {
vis[to]=1;
q.push(to);
}
}
}
}
if(dis[t]==INF) return false;
return true;
}
void dinic(int s,int t) {
int maxflow=0;
int mincost=0;
while(spfa(s,t)) {
int x=t;
maxflow+=incf[t];
mincost+=dis[t]*incf[t];
while(x!=s) {
int i=pre[x];
E[i].flow-=incf[t];
E[i^1].flow+=incf[t];
x=E[i^1].to;
}
if(maxflow==2) break; // The maximum flow is 2 It indicates that two to from have been found 1-n The shortest circuit of
}
//if(maxflow<2) cout << "-1" << endl; Because the data given by the topic must have two shortest paths , So this sentence is useless ,,,,,
cout << mincost << endl;
}
void init() {
tot=1;
memset(head,-1,sizeof(head));
}
int main() {
ios::sync_with_stdio(false);
init();
cin >> n >> m;
for(int i=0;i<m;i++) {
int u,v,w,x;
cin >> u >> v >> w;
// Because it's an undirected graph , So it's equivalent to adding 4 side
add(u,v,1,w);
add(v,u,0,-w);
add(v,u,1,w);
add(u,v,0,-w);
}
dinic(1,n);
return 0;
}
边栏推荐
- Go coding specification
- Packaging_ Conversion between basic type and string type
- The Google File System (GFS) learning notes
- Simpledateformat thread unsafe
- Network layer
- Second understanding permutation and combination
- Bridging mode -- law firm
- More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
- C langage pour le déminage (version simplifiée)
- An example illustrates restful API
猜你喜欢

Several common command operations in win system

Image panr

Enjoy yuan mode -- a large number of flying dragons

The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time

What does virtualization mean? What technologies are included? What is the difference with private cloud?

Record a deletion bash_ Profile file

I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world

Summary of message protocol problems

JMeter basic learning records

More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
随机推荐
data link layer
Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
Record a deletion bash_ Profile file
Rip/ospf protocol notes sorting
What will you do if you have been ignored by your leaders at work?
More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
Pytest test framework II
Leetcode (455) - distribute cookies
Where is 5g really powerful? What is the difference with 4G?
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
2021-09-30
Leetcode(135)——分发糖果
[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
IDEA Dashboard
Limit summary (under update)
Subnet partition operation
Sequence stack version 1.0
Difference between map and object
I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world
畅直播|针对直播痛点的关键技术解析