Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4269 | Accepted: 2004 |
Description
Input
Output
Sample Input
0 0 1 0 3 3 (0,1) (0,2) (1,2) 2 0 5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)
Sample Output
0 1 3 0 2
Hint
//POJ1966 #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 100+10 #define MAXM 20000+10 #define INF 0x3f3f3f3f using namespace std; struct Edge { int from, to, cap, flow, next; }; Edge edge[MAXM], Redge[MAXM]; int head[MAXN], Rhead[MAXN], edgenum; int N, M; int dist[MAXN], cur[MAXN]; bool vis[MAXN]; void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { for(int i = head[u]; i != -1; i = edge[i].next) { if(edge[i].to == v) return ; } Edge E1 = {u, v, w, 0, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } int a[MAXM], b[MAXM]; void input() { for(int i = 0; i < M; i++) scanf(" (%d,%d)", &a[i], &b[i]), a[i]++, b[i]++; } void getMap(int s, int t) { init(); for(int i = 0; i < M; i++) { addEdge(a[i]+N, b[i], INF); addEdge(b[i]+N, a[i], INF); } for(int k = 1; k <= N; k++) (k != s && k != t) ? addEdge(k, k+N, 1) : addEdge(k, k+N, INF); } bool BFS(int s, int t) { queue<int> Q; memset(dist, -1, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[s] = 0; vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && E.cap > E.flow) { dist[E.to] = dist[u] + 1; if(E.to == t) return true; vis[E.to] = true; Q.push(E.to); } } } return false; } int DFS(int x, int a, int t) { if(x == t || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next) { Edge &E = edge[i]; if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0) { edge[i].flow += f; edge[i^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int s, int t) { int flow = 0; while(BFS(s, t)) { memcpy(cur, head, sizeof(head)); flow += DFS(s, INF, t); } return flow; } void solve() { int ans = N; for(int i = 1; i <= N; i++)//枚举源点 { for(int j = i+1; j <= N; j++)//汇点 { getMap(i, j); ans = min(ans, Maxflow(i, j));//更新 } } printf("%d\n", ans); } int main() { while(scanf("%d%d", &N, &M) != EOF) { input(); solve(); } return 0; }