QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#423694 | #2208. Flow | fzj2007 | RE | 0ms | 0kb | C++14 | 2.1kb | 2024-05-28 15:15:57 | 2024-05-28 15:15:57 |
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
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<<2];
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(){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s),dis[s]=0,vis[s]=1,incf[s]=0x3f3f3f3f;
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(e[i].flow&&dis[v]>dis[x]+e[i].cost){
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]<0x3f3f3f3f;
}
inline int mcmf(){
int res=0;
while(spfa()){
res+=dis[t]*incf[t];
for(int x=t,i=pre[x];x!=s;x=e[i^1].v,i=pre[x])
e[i].flow-=incf[t],e[i^1].flow+=incf[t];
}
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;
}
详细
Test #1:
score: 0
Runtime Error