弱智求条板子
查看原帖
弱智求条板子
781159
Lovely_Elaina楼主2024/8/14 20:12
#include <bits/stdc++.h>
//#define int long long
//#define double long double
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define endl '\n' 
using namespace std;
typedef long long ll;
const int N = 2e2+5;
const int M = 5e3+5;
const int INF = 0x7f7f7f7f;

struct node{
	int next,to;
	ll value;
}a[M];
int k,pre[N];

int n,m,s,t,u,v,last[N];
map<pair<int,int >,int > table;
ll dfs[N],w,dis[M],ans;
bool vis[M];

inline void add(int u,int v,int w){
	a[++k] = {pre[u],v,w};
	pre[u] = k;
	a[++k] = {pre[v],u,0};
	pre[v] = k;
}

inline bool bfs(){
	memset(vis,0,sizeof(vis));
	
	queue<int > q;
	q.push(s);
	vis[s] = true;
	dis[s] = INF;
	
	while(!q.empty()){
		int f = q.front();
		q.pop();
		
		for(int i = pre[f]; i; i=a[i].next){
			if(!a[i].value) continue;
			int to = a[i].to;
			if(vis[to]) continue;
			
			dis[to] = min(dis[f],a[i].value);
			last[to] = i,vis[v] = true,q.push(to);
			if(to == t) return true;
		}
	}
	return false;
}

inline void update(){
	int x = t;
	while(x != s){
		int l = last[x];
		a[l].value -= dis[t];
		a[l^1].value += dis[t];
		x = a[l^1].to;
	}
	ans += dis[t];
} 

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	
	freopen("app.in","r",stdin);
//	freopen("app.out","w",stdout);
	
	pair<int,int > p;
	
	cin >> n >> m >> s >> t;
	
	for(int i = 1; i <= m; i++){
		cin >> u >> v >> w;
		p.first = u,p.second = v;
		
		if(table.find(p) == table.end()){
			add(u,v,w);
			table[p] = k;
		}else a[table[p]-1].value += w;
	}
	
//	for(int i = 1; i <= n; i++){
//		cout << i << " : ";
//		if(!pre[i]) cout << "null";
//		for(int j = pre[i]; j; j=a[j].next)
//			cout << a[j].to << " ";
//		cout << endl;
//	} 
	
	while(bfs()) update();
	cout << ans << endl;
	return 0; 
} 

邻接表没问题 qqq

疯了 qqqq

线段树可爱 qqqqute

2024/8/14 20:12
加载中...