标签:ssi pac double inf -- script lin color end
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 254 Accepted Submission(s): 80
【分析】费用流模板题。不过在
BellmanFord函数里面加一句话
if(d[t]<0) return false;
保证跑最大流的时候不会亏本。
#include <bits/stdc++.h> #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define met(a,b) memset(a,b,sizeof a) using namespace std; typedef vector<int> vi; typedef long long ll; typedef pair<int,int> pii; const ll mod=1000000007; const int inf=(1<<30); const double pi=acos(-1); ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} inline void pcas(int ca) {printf("Case %d: ",ca);} int n,m; const int maxn=500+50; struct Edge { int from, to, cap, flow; int cost; }; inline int Min(int aa,int bb) { return aa<bb?aa:bb; } struct MCMF { int n, m, s, t; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn]; // 是否在队列中 int d[maxn]; // Bellman-Ford int p[maxn]; // 上一条弧 int a[maxn]; // 可改进量 void init(int n) { this->n = n; for(int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap, int cost) { edges.push_back((Edge){from, to, cap, 0, cost}); edges.push_back((Edge){to, from, 0, 0, -cost}); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BellmanFord(int s, int t, int& flow,int& cost) { for(int i = 0; i < n; i++) d[i] = -10000000; memset(inq, 0, sizeof(inq)); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = inf; queue<int> Q; Edge e; int l,u; Q.push(s); while(!Q.empty()) { u = Q.front(); Q.pop(); inq[u] = 0; l=G[u].size(); for(int i = 0; i < l; i++) { e = edges[G[u][i]]; if(e.cap > e.flow && d[e.to]<d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = G[u][i]; a[e.to] = Min(a[u], e.cap - e.flow); if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; } } } } if(d[t]<0) return false; cost += d[t]*a[t]; u = t; while(u != s) { edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; u = edges[p[u]].from; } return true; } // 需要保证初始网络中没有负权圈 int Mincost(int s, int t) { int cost = 0; int flow=0; while(BellmanFord(s, t,flow, cost)); return cost; } }; int a[maxn],b[maxn],c[maxn],d[maxn]; MCMF ac; int main() { int n,m; while(~scanf("%d%d",&n,&m)){ int a,b,c,d; ac.init(n+2); for (int i=1; i<=n; ++i) { scanf("%d%d%d%d",&a,&b,&c,&d); ac.AddEdge(0,i,b,-a); ac.AddEdge(i,n+1,d,c); } while (m--) { scanf("%d%d%d",&a,&b,&c); ac.AddEdge(a,b,inf,-c); ac.AddEdge(b,a,inf,-c); } printf("%d\n",ac.Mincost(0,n+1)); } return 0; }
HDU 6118 2017百度之星初赛B 度度熊的交易计划(费用流)
标签:ssi pac double inf -- script lin color end
原文地址:http://www.cnblogs.com/jianrenfang/p/7354464.html