POJ 3469 Dual Core CPU 最大流
划分成两个集合使费用最小,可以转成最小割,既最大流。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("data.in","r",stdin); // freopen("d:\\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF; } struct Edge { int to,cap; }; const int maxn = 20010; vector<int> g[maxn]; vector<Edge> edge; int n,m,s,t; int d[maxn]; int vis[maxn]; int cur[maxn]; void add(int u,int v,int cap) { //printf("%d -> %d : %d\n", u, v, cap); edge.push_back((Edge){v, cap}); g[u].push_back(edge.size() - 1); edge.push_back((Edge){u, 0}); g[v].push_back(edge.size() - 1); } void build() { for(int i = 1; i <= n; i++) g[i].clear(); edge.clear(); s = n + 1; t = s + 1; for(int i = 1; i <= n; i++) { int a,b; scanf("%d%d", &a, &b); add(s, i, a); add(i, t, b); } for(int i = 1; i <= m; i++) { int a,b,w; scanf("%d%d%d", &a, &b, &w); add(a, b, w); add(b, a, w); } } bool bfs() { memset(vis, 0, sizeof(vis)); d[s] = 0; queue<int> q; q.push(s); vis[s] = true; while(!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < g[x].size(); i++) { Edge &e = edge[g[x][i]]; if(e.cap>0 && !vis[e.to]) { vis[e.to] = true; q.push(e.to); d[e.to] = d[x] + 1; } } } return vis[t]; } int dfs(int u,int f) { if(u==t || f==0) return f; for(int &i = cur[u]; i < g[u].size(); i++) { Edge &e = edge[g[u][i]]; if(e.cap>0 && d[e.to] == d[u] + 1) { int d = dfs(e.to, min(f,e.cap)); if(d>0) { e.cap -= d; edge[g[u][i]^1].cap += d; return d; } } } return 0; } int max_flow() { int res = 0; while(bfs()) { memset(cur, 0, sizeof(cur)); int d; while(d=dfs(s,INF)) { res += d; } } return res; } int main() { debug(); while(scanf("%d%d", &n, &m) != EOF) { build(); printf("%d\n", max_flow()); } return 0; }