QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#423704 | #2208. Flow | fzj2007 | WA | 71ms | 3900kb | C++14 | 2.2kb | 2024-05-28 15:20:57 | 2024-05-28 15:21:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &x){
x=0;
bool flag=0;
char ch=getchar();
while(ch<'0'||ch>'9') flag=flag||(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
x=flag?-x:x;
}
template<typename T,typename ...Args>inline void read(T &x,Args &...args){
read(x),read(args...);
}
template<typename T>inline void prt(T x){
if(x>9) prt(x/10);
putchar(x%10+'0');
}
template<typename T>inline void put(T x){
if(x<0) putchar('-'),x=-x;
prt(x);
}
template<typename T>inline void put(char ch,T x){
put(x),putchar(ch);
}
template<typename T,typename ...Args>inline void put(char ch,T x,Args ...args){
put(ch,x),put(ch,args...);
}
#define N 2024
#define inf 0x3f3f3f3f
struct Flow{
int head[N],cnt,s,t;
Flow(){
memset(head,0,sizeof(head));
memset(e,0,sizeof(e));
cnt=1;
}
struct edge{
int v,nxt,flow,cost;
}e[N<<3];
inline void adde(int u,int v,int flow,int cost){
e[++cnt]=(edge){v,head[u],flow,cost},head[u]=cnt;
}
inline void add(int u,int v,int flow,int cost){
adde(u,v,flow,cost),adde(v,u,0,-cost);
}
int dis[N],vis[N],incf[N],pre[N];
inline bool spfa(){
queue<int> q;
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(pre,0,sizeof(pre));
memset(incf,0,sizeof(incf));
q.push(s);
dis[s]=0,vis[s]=1,incf[s]=inf;
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=0;
for(int i=head[x];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]>dis[x]+e[i].cost&&e[i].flow){
dis[v]=dis[x]+e[i].cost;
incf[v]=min(incf[x],e[i].flow);
pre[v]=i;
if(!vis[v])
vis[v]=1,q.push(v);
}
}
}
return dis[t]<inf;
}
inline int mcmf(){
int res=0;
while(spfa()){
int x=t;
res+=incf[t]*dis[t];
while(x!=s){
int i=pre[x];
e[i].flow-=incf[t],e[i^1].flow+=incf[t];
x=e[i^1].v;
}
}
return res;
}
}F;
int n,m,deg[N],ans;
int main(){
read(n,m);
for(int i=1,u,v,w;i<=m;i++){
read(u,v,w),F.add(v,u,w,1),ans+=w;
deg[u]+=w,deg[v]-=w;
}
F.s=0,F.t=n+1;
for(int i=1;i<=n;i++)
if(deg[i]>0) F.add(F.s,i,deg[i],0);
else if(deg[i]<0) F.add(i,F.t,-deg[i],0);
put('\n',ans-F.mcmf());
return 0;
}
Details
Test #1:
score: 0
Wrong Answer
time: 71ms
memory: 3900kb