ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
阿新 • • 發佈:2019-02-17
連結 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26746
題目意思有點兒難描述 用一個別人描述好的。
我的建圖方法:一個源點一個匯點,和所有種類的插座。輸入的n個插座直接與源點相連,容量為1,m個物品輸入裡 記錄每個插座對應的物品個數,物品數然後大於0的插座直接連到匯點,意味著最終的物品只能由這些插座流出。中間的插座轉換容量都是INF a b表示 無論多少b都可以選擇轉化到a。
/*--------------------- #headfile--------------------*/ #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cstdlib> #include <cassert> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> /*----------------------#define----------------------*/ #define DRII(X,Y) int (X),(Y);scanf("%d%d",&(X),&(Y)) #define EXP 2.7182818284590452353602874713527 #define CASET int _;cin>>_;while(_--) #define RII(X, Y) scanf("%d%d",&(X),&(Y)) #define DRI(X) int (X);scanf("%d", &X) #define mem(a,b) memset(a,b,sizeof(a)) #define rep(i,n) for(int i=0;i<n;i++) #define ALL(X) (X).begin(),(X).end() #define INFL 0x3f3f3f3f3f3f3f3fLL #define RI(X) scanf("%d",&(X)) #define SZ(X) ((int)X.size()) #define PDI pair<double,int> #define rson o<<1|1,m+1,r #define PII pair<int,int> #define MAX 0x3f3f3f3f #define lson o<<1,l,m #define MP make_pair #define PB push_back #define SE second #define FI first typedef long long ll; template<class T>T MUL(T x,T y,T P){T F1=0;while(y){if(y&1){F1+=x;if(F1<0||F1>=P)F1-=P;}x<<=1;if(x<0||x>=P)x-=P;y>>=1;}return F1;} template<class T>T POW(T x,T y,T P){T F1=1;x%=P;while(y){if(y&1)F1=MUL(F1,x,P);x=MUL(x,x,P);y>>=1;}return F1;} template<class T>T gcd(T x,T y){if(y==0)return x;T z;while(z=x%y)x=y,y=z;return y;} #define DRIII(X,Y,Z) int (X),(Y),(Z);scanf("%d%d%d",&(X),&(Y),&(Z)) #define RIII(X,Y,Z) scanf("%d%d%d",&(X),&(Y),&(Z)) const double pi = acos(-1.0); const double eps = 1e-6; const ll mod = 1000000007ll; const int M = 1005; const int N = 605; using namespace std; /*----------------------Main-------------------------*/ struct Edge { int to, c, rev; Edge() {} Edge(int _to, int _c, int _rev) { to = _to, c = _c, rev = _rev; } }; vector<Edge> G[N]; int lv[N], iter[N]; int n, m; void BFS(int s) { mem(lv, -1); queue<int> q; lv[s] = 0; q.push(s); while(!q.empty()) { int v = q.front(); q.pop(); for(int i = 0; i < SZ(G[v]); i++) { Edge &e = G[v][i]; if(e.c > 0 && lv[e.to] < 0) { lv[e.to] = lv[v] + 1; q.push(e.to); } } } } int dfs(int v, int t, int f) { if(v == t) return f; for(int &i = iter[v]; i < SZ(G[v]); i++) { Edge &e = G[v][i]; if(e.c > 0 && lv[v] < lv[e.to]) { int d = dfs(e.to, t, min(f, e.c)); if(d > 0) { e.c -= d; G[e.to][e.rev].c += d; return d; } } } return 0; } int MF(int s, int t) { int res = 0; for( ; ; ) { BFS(s); if(lv[t] < 0) return res; mem(iter, 0); int f; while((f = dfs(s, t, 1e9)) > 0) { res += f; } } } void add(int from, int to, int c) { G[from].PB( Edge(to, c, SZ(G[to])) ); G[to].PB( Edge(from, 0, SZ(G[from]) - 1) ); } int num[N]; int FF = 0; void solve() { if(FF) puts(""); FF = 1; RI(n); for(int i = 0; i < 300; i++) G[i].clear(); mem(num, 0); int s = 0, k = 0; map<string, int> vis; for(int i = 1; i <= n; i++) { string s1; cin >> s1; vis[s1] = ++k; add(s, i, 1); } RI(m); for(int i = 1; i <= m; i++) { string s1, s2; cin >> s1 >> s2; if(vis[s2] == 0) vis[s2] = ++k; num[ vis[s2] ]++; } int t = k + 1; for(int i = 1; i <= k; i++) { if(num[i]) add(i, t, num[i]); } DRI(x); k++; for(int i = 1; i <= x; i++) { string s1, s2; cin >> s1 >> s2; if(vis[s2] == 0) vis[s2] = ++k; if(vis[s1] == 0) vis[s1] = ++k; int u = vis[s2], v = vis[s1]; add(u, v, 1e9); } int ans = MF(s, t); printf("%d\n", m - ans); } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); CASET solve(); return 0; }