社区讨论
细思极恐。。。
灌水区参与者 4已保存回复 12
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 12 条
- 当前快照
- 1 份
- 快照标识符
- @lo8zdhel
- 此快照首次捕获于
- 2023/10/28 03:02 2 年前
- 此快照最后确认于
- 2023/10/28 03:02 2 年前
CPP
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e6;
int dis[N],pre[N],preve[N],h[N];
int n,m;
int s,t;
struct edge{
int to,cost,capa,rev;
edge(int to_,int cost_,int c,int rev_)
{
to=to_;
cost=cost_;
capa=c;
rev=rev_;
}
};
struct edeg{
int v,nxt;
ll cap,flow;
}eh[N<<1];
int cnt;
//void addedge(int u,int v,int w)
//{
// eh[cnt].v =v;
// eh[cnt].cap =w;
// eh[cnt].flow =0;
// eh[cnt].nxt =h[u];
// h[u]=cnt++;
//}
vector<edge> e[N];
void add(int from,int to,int cost,int capa)
{
e[from].push_back(edge(to,cost,capa,e[to].size()));
e[to].push_back(edge(from,-cost,0,e[from].size()-1));
}
bool spfa(int s,int t,int cnt)
{
bool inq[N];
memset(pre,-1,sizeof(pre));
for(int i=1;i<=cnt;i++)
{
dis[i]=inf;
inq[i]=false;
}
dis[s]=0;
queue<int> Q;
Q.push(s);
inq[s]=true;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=false;
for(int i=0;i<e[u].size();i++)
{
if(e[u][i].capa>0)
{
int v=e[u][i].to,cost=e[u][i].cost;
if(dis[u]+cost<dis[v])
{
dis[v]=dis[u]+cost;
pre[v]=u;
preve[v]=i;
if(!inq[v])
{
inq[v]=true;
Q.push(v);
}
}
}
}
}
return dis[t]!=inf;
}
int maxflow;
int mincost(int s,int t,int cnt)
{
int cost=0;
while(spfa(s,t,cnt))
{
int v=t;
int flow=inf;
while(pre[v]!=-1)
{
int u=pre[v],i=preve[v];
flow=min(flow,e[u][i].capa);
v=u;
}
maxflow+=flow;
v=t;
while(pre[v]!=-1)
{
int u=pre[v];
int i=preve[v];
e[u][i].capa=e[u][i].capa-flow;
e[v][e[u][i].rev].capa+=flow;
v=u;
}
cost+=dis[t]*flow;
}
return cost;
}
//int vis[N];
//int preh[N];
//bool bfs()
//{
// memset(preh,-1,sizeof(preh));
// memset(vis,0,sizeof(vis));
// queue<int>q;
// vis[s]=1;
// q.push(s);
// while(!q.empty())
// {
// int u=q.front();
// q.pop();
// for(int i=h[u];~i;i=eh[i].nxt )
// {
// int v=eh[i].v ;
// if(!vis[v]&&eh[i].cap >eh[i].flow )
// {
// vis[v]=1;
// preh[v]=i;
// q.push(v);
// if(v==t) return 1;
// }
// }
// }
// return 0;
//}
//int EK()
//{
// ll maxflow=0;
// while(bfs())
// {
// ll d=inf;
// int v=t;
// while(v!=s)
// {
// int i=preh[v];
// d=min(d,eh[i].cap -eh[i].flow );
// v=eh[i^1].v ;
// }
// maxflow+=d;
// v=t;
// while(v!=s)
// {
// int i=preh[v];
// eh[i].flow +=d;
// eh[i^1].flow -=d;
// v=eh[i^1].v ;
// }
// }
// return maxflow;
//}
int main()
{
memset(h,-1,sizeof(h));
cin>>n>>m>>s>>t;
for(int i=1;i<=m;i++)
{
int u,v,w,c;
cin>>u>>v>>w>>c;
add(u,v,c,w);
add(v,u,-c,0);
// addedge(u,v,w);
// addedge(v,u,0);
}
cout<<maxflow<<" "<<mincost(s,t,n);
}
同一段代码,
同一个样例
洛谷IDE输出:0 43371
本地输出:237 43371
救命。。。
回复
共 12 条回复,欢迎继续交流。
正在加载回复...