BZOJ 1497: [NOI2006]最大获利(最大权闭合图)

http://www.lydsy.com/JudgeOnline/problem.php?id=1497

题意:

BZOJ 1497: [NOI2006]最大获利(最大权闭合图)

思路:

论文题,只要看过论文的话就是小菜一碟啦~

每个用户群i作为一个结点分别向相应的中转站ai和中转站bi连有向边。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, m;
int val[maxn]; struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
for(int i=;i<n;++i) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();++i)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow=, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+ && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>)
{
e.flow +=f;
edges[G[x][i]^].flow -=f;
flow +=f;
a -=f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow +=DFS(s,INF);
}
return flow;
}
}DC; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
int src = , dst = n+m+;
DC.init(dst+);
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
DC.AddEdge(i,dst,val[i]);
}
int sum = ;
for(int i=;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
DC.AddEdge(src,i+n,c);
DC.AddEdge(i+n,a,INF);
DC.AddEdge(i+n,b,INF);
sum+=c;
}
int ans = sum-DC.Maxflow(src,dst);
printf("%d\n",ans);
}
return ;
}
上一篇:#力扣 LeetCode剑指 Offer 10- II. 青蛙跳台阶问题 #在所有 Java 提交中击败了 100.00% 的用户 @FDDLC


下一篇:python中的break\return\pass\continue用法