POJ 1637 Sightseeing Tour
Topic links
Test instructions: Give some non-directional edges, ask if you can orient the non-directed side to determine a Euler loop
Idea: The model of this problem is very ingenious, turn a http://blog.csdn.net/pi9nc/article/details/12223693
First of all to the direction of arbitrary direction, and then according to the degree of each point of the difference in degrees, can determine the number of times each point needs to be adjusted, and then the middle is the need to adjust the side, the capacity of 1, so to build the final inference from the source point is full flow can be
Code:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std; const int maxnode = 205;const int MAXEDGE = 10005;typedef int type;const Type INF = 0x3f3f3f3f;struct Edge {int u, v; Type cap, Flow; Edge () {}edge (int u, int v, type cap, type flow) {This->u = U;this->v = V;this->cap = Cap;this->flow = flow;} };struct dinic {int n, m, S, t; Edge edges[maxedge];int first[maxnode];int Next[maxedge];bool Vis[maxnode]; Type d[maxnode];int cur[maxnode];vector<int> cut;void init (int n) {this->n = N;memset (First,-1, sizeof (first)) ; m = 0;} void Add_edge (int u, int v, Type cap) {Edges[m] = Edge (U, V, cap, 0); Next[m] = first[u];first[u] = M++;edges[m] = Edge (V, U, 0, 0); Next[m] = first[v];first[v] = m++;} BOOL BFs () {memset (Vis, false, sizeof (VIS));queue<int> Q; Q.push (s);d [s] = 0;vis[s] = True;while (! Q.empty ()) {int u = q.front (); Q.pop (); for (int i = first[u]; I! = 1; i = Next[i]) {edge& e = edges[i];if (!VIS[E.V] &&Amp E.cap > E.flow) {vis[e.v] = true;d[e.v] = D[u] + 1; Q.push (E.V);}}} return vis[t];} Type DFS (int u, Type a) {if (U = = T | | a = = 0) return A; Type flow = 0, f;for (int &i = Cur[u]; i = 1; i = Next[i]) {edge& e = edges[i];if (D[u] + 1 = d[e.v] && (f = DFS (e.v, Min (A, e.cap-e.flow))) > 0) {e.flow + = F;edges[i^1].flow = F;flow + F;a = f;if (A = = 0) break;} return flow;} BOOL Maxflow (int s, int t) {this->s = s; this->t = t; Type flow = 0;while (BFS ()) {for (int i = 0; i < n; i++) Cur[i] = First[i];flow + = DFS (s, INF);} for (int i = first[0]; i + 1; i = Next[i]) if (edges[i].flow! = Edges[i].cap) return False;return true;} void Mincut () {cut.clear (); for (int i = 0; i < m; i + = 2) {if (vis[edges[i].u] &&!vis[edges[i].v]) Cut.push_bac K (i);}}} gao;const int N = 205;const int M = 1005;int T, N, M, In[n], Out[n];int u[m], v[m], W[m];bool solve () {Gao.init (N + 2); for (int i = 1; I <= n; i++) {if ((In[i] + out[i])% 2) return false;if (In[i] > out[I]) Gao.add_edge (i, n + 1, (In[i]-out[i])/2), if (Out[i] > In[i]) gao.add_edge (0, I, (Out[i]-in[i])/2);} for (int i = 0; i < m; i++) {if (W[i]) Continue;gao.add_edge (U[i], v[i], 1);} Return Gao. Maxflow (0, n + 1);} int main () {scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m), memset (in, 0, sizeof (in)), memset (out, 0, Sizeo F (out)); (int i = 0; i < m; i++) {scanf ("%d%d%d", &u[i], &v[i], &w[i]); in[v[i]]++;out[u[i]]++;} printf ("%s\n", Solve ()? "Possible": "Impossible");} return 0;}
POJ 1637 Sightseeing tour (Max Stream)