USACO Pollutant Control

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 ID: wcr19961
PROG: milk6
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

typedef long long LL;
const LL inf = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 110;
const int maxm = 1010;
int ans[maxm];
int u[maxm], v[maxm], c[maxm];
LL cap[maxm];

struct Edge {
int from, to;
LL cap, flow;
Edge(int u, int v, LL c, LL f): from(u), to(v), cap(c), flow(f) {}
};

bool operator < (const Edge& a, const Edge& b) {
return a.from < b.from || (a.from == b.from && a.to < b.to);
}

struct ISAP {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn]; // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; // BFS使用
int d[maxn]; // 从起点到i的距离
int cur[maxn]; // 当前弧指针
int p[maxn]; // 可增广路上的上一条弧
int num[maxn]; // 距离标号计数

void AddEdge(int from, int to, LL cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = int(edges.size());
G[from].push_back(m-2);
G[to].push_back(m-1);
}

bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(t);
vis[t] = 1;
d[t] = 0;
while(!Q.empty()) {
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]^1];
if(!vis[e.from] && e.cap > e.flow) {
vis[e.from] = 1;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}

void ClearAll(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}

void ClearFlow() {
for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
}

LL Augment() {
LL x = t, a = inf;
while(x != s) {
Edge& e = edges[p[x]];
a = min(a, e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x != s) {
edges[p[x]].flow += a;
edges[p[x]^1].flow -= a;
x = edges[p[x]].from;
}
return a;
}

LL Maxflow(int s, int t) {
this->s = s; this->t = t;
LL flow = 0;
BFS();
memset(num, 0, sizeof(num));
for(int i = 0; i < n; i++) num[d[i]]++;
int x = s;
memset(cur, 0, sizeof(cur));
while(d[s] < n) {
if(x == t) {
flow += Augment();
x = s;
}
int ok = 0;
for(int i = cur[x]; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if(e.cap > e.flow && d[x] == d[e.to] + 1) { // Advance
ok = 1;
p[e.to] = G[x][i];
cur[x] = i; // 注意
x = e.to;
break;
}
}
if(!ok) { // Retreat
int m = n-1; // 初值注意
for(int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if(e.cap > e.flow) m = min(m, d[e.to]);
}
if(--num[d[x]] == 0) break;
num[d[x] = m+1]++;
cur[x] = 0; // 注意
if(x != s) x = edges[p[x]].from;
}
}
return flow;
}
} isap;


int main() {
freopen("milk6.in", "r", stdin);
freopen("milk6.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
isap.ClearAll(n + 1);
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &u[i], &v[i], &c[i]);
cap[i] = LL(c[i]) * 1001 + 1;
isap.AddEdge(u[i], v[i], cap[i]);
}
LL aim = isap.Maxflow(1, n);
int cost = aim / 1001, num = aim % 1001;
printf("%d %d\n", cost, num);
int cnt = 0;
for (int i = 0; i < m && cnt < num; ++i) {
isap.ClearFlow();
isap.edges[i << 1].cap = 0;
LL tmp = isap.Maxflow(1, n);
if (tmp + cap[i] == aim) {
aim -= cap[i];
ans[cnt++] = i + 1;
} else {
isap.edges[i << 1].cap = cap[i];
}
}
for (int i = 0; i < num; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}