# 洛谷P2762 太空飞行计划问题 网络流

  1 #include<bits/stdc++.h>
2 #define LL long long
3 #define GG int
4 #define For(i, j, k) for(int i=j; i<=k; i++)
5 #define Dow(i, j, k) for(int i=j; i>=k; i--)
6 using namespace std;
8     GG x = 0, f = 1;
9     char ch = getchar();
10     while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); }
11     while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); }
12     return x * f;
13 }
14 inline void write(GG x) {
15     if(x<0) putchar('-'), x = -x;
16     if(x>9) write(x/10);
17     putchar(x%10+48);
18 }
19 inline void writeln(GG x) {
20     write(x); puts("");
21 }
22
23 const int N = 61, M = 4011, INF = 1e9;
24 struct edge{
25     int to, pre, val;
26 }e[M*2];
28 int nedge;
29
30 inline void add(int x, int y, int v) {
31     e[++nedge] = (edge){ y, head[x], v};
33 }
34
35 namespace work_1 {
36     int n, m, S, T;
37     int dep[N*2];
38     inline void pre(int nn, int mm, int ss, int tt) {
39         n = nn; m = mm; S = ss; T = tt;
40     }
41     inline int bfs() {
42         For(i, 0, n) dep[i] = 0;
43         memset(vis, 0, sizeof vis);
44         queue<int> Q;
45         Q.push(S);
46         dep[S] = 1;
47         while(!Q.empty()) {
48             int u = Q.front(); Q.pop();
49             for(int i=head[u]; i!=-1; i=e[i].pre) {
50                 int v = e[i].to;
51                 if(e[i].val>0 && dep[v]==0) {
52                     dep[v] = dep[u]+1;
53                     vis[v] = 1;
54                     Q.push(v);
55                 }
56             }
57         }
58         return dep[T];
59     }
60     int dfs(int u, int dist) {
61         if(u == T) return dist;
62         for(int& i=cur[u]; i!=-1; i=e[i].pre) {
63             int v = e[i].to;
64             if(e[i].val>0 && dep[v]==dep[u]+1) {
65                 int flow = dfs(v, min(e[i].val, dist));
66                 if(flow > 0) {
67                     e[i].val -= flow;
68                     e[i^1].val += flow;
69                     return flow;
70                 }
71             }
72         }
73         return 0;
74     }
75     inline int Dinic() {
76         int ans = 0;
77         while( bfs() ) {
78             for(int i=0; i<=n; i++) cur[i] = head[i];
79             while(int d = dfs(S, INF))
80                 ans += d;
81         }
82         return ans;
83     }
84 }
85
86 int main() {
88     nedge = -1;
89     int sum = 0;
91     For(i, 1, m) {
93         sum += x;
95         char ch;
96         while((ch=getchar()) != '\n') {
97             x = ch-'0';
98             while((ch=getchar())&&ch>='0'&&ch<='9')
99                 x=x*10+ch-'0';
101             if(ch=='\n') break;
102         }
103     }
104     For(i, 1, n) {
107     }
108     work_1 :: pre(n+m+1, nedge, 0, n+m+1);
109     int ans = work_1 :: Dinic();
110     ans = sum - ans;
111     For(i, 1, m) if(vis[i]) printf("%d ",i);
112     puts("");
113     For(i, 1, n) if(vis[i+m]) printf("%d ",i);
114     puts("");
115     writeln(ans);
116 }

posted @ 2018-04-08 15:41  third2333  阅读(117)  评论(0编辑  收藏  举报