当前位置:网站首页>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;
}
边栏推荐
- Dx12 engine development course progress - where does this course go
- Memo mode - game archiving
- JMeter response assertion
- Nifi quick installation (stand-alone / cluster)
- Variable setting in postman
- Interpreter mode -- formulas for dating
- After a few years in the testing industry, do you still know a little?
- Simulation lottery and probability statistics experiment of the top 16 Champions League
- Design of routing service for multi Activity Architecture Design
- Common data model (updating)
猜你喜欢

maptalks:数据归一化处理与分层设色图层加载

Procedural life: a few things you should know when entering the workplace

Read all text from stdin to a string

Implement the redis simple client customized based on socket

使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value

Design of routing service for multi Activity Architecture Design

After idea installs these plug-ins, the code can be written to heaven. My little sister also has to arrange it

Leetcode(135)——分发糖果

yeb_ Back first day

主数据建设的背景
随机推荐
After idea installs these plug-ins, the code can be written to heaven. My little sister also has to arrange it
Leetcode (146) - LRU cache
Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
JMeter basic learning records
What does virtualization mean? What technologies are included? What is the difference with private cloud?
微信小程序自定义tabBar
DAPP system customization of full chain hash game (scheme design)
How to enhance influence
VIM usage
A/B测试助力游戏业务增长
After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked
An example illustrates restful API
Vant component used in wechat applet
Reflect package
使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value
Haitai Advanced Technology | application of privacy computing technology in medical data protection
Geek University cloud native training camp
JMeter response assertion
Can the OPDS SQL component pass process parameters to the next component through context
Batch capitalization of MySQL table names