Rt
样例2没过,粘在注释里了
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll maxn=3e5+10,INF=1e11;
ll n,m,s,t,cnt=2,ans;//
ll fir[maxn],deep[maxn],now[maxn];
queue<ll>q;
struct node{ll to,w,nxt;}e[maxn];
struct Gragh{ll u,v,w;}g[maxn];
bool cmp(Gragh x,Gragh y){return x.w>y.w;}
void add_edge(ll a,ll b,ll c){
cnt+=2;
e[cnt].to=b;
e[cnt].w=c;
e[cnt].nxt=fir[a];
fir[a]=cnt;
}
bool bfs(){
for(int i=1;i<=n;i++)deep[i]=INF;//
while(!q.empty())q.pop();//
deep[s]=0;
q.push(s);
now[s]=fir[s];
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=fir[x];i;i=e[i].nxt){
int v=e[i].to;
if(e[i].w>0&&deep[v]==INF){
deep[v]=deep[x]+1;
q.push(v);
now[v]=fir[v];
if(v==t)return 1;
}
}
}
return 0;
}
ll dfs(ll x,ll sum){
if(x==t)return sum;
ll k,res=0;
for(int i=now[x];i&∑i=e[i].nxt){
now[x]=i;
int v=e[i].to;
if(e[i].w>0&&deep[v]==deep[x]+1){
k=dfs(v,min(sum,e[i].w));
if(!k)deep[v]=INF;//
e[i].w-=k;
e[i^1].w+=k;
res+=k;
sum-=k;
if(!sum)break;
}
}
if(!res)deep[x]=INF;
return res;
}
int main(){
cin>>n>>m>>s>>t;
for(ll i=1;i<=m;i++)cin>>g[i].u>>g[i].v>>g[i].w;
sort(g+1,g+1+m,cmp);
for(int f=0;f<2;f++){
if(f)cnt=3;
for(int k=5,p=1;k>=0;k--){
while(p<=m&&g[p].w>=(1<<(k*6))){
if(!f)add_edge(g[p].u,g[p].v,g[p].w);
else add_edge(g[p].v,g[p].u,0);
p++;
}
while(bfs())ans+=dfs(s,INF);
}
}
cout<<ans;
return 0;
}
/*
10 16 1 2
1 3 2
1 4 2
5 2 2
6 2 2
3 5 1
3 6 1
4 5 1
4 6 1
1 7 2147483647
9 2 2147483647
7 8 2147483647
10 9 2147483647
8 5 2
8 6 2
3 10 2
4 10 2
*/