Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2770 航空路线问题

P2770 航空路线问题

如果直接按照题意从 $1$ 到 $n$ 再到 $1$ ,形成正环,没法跑。

实际上,相当与从 $1$ 到 $n$ 跑两次。

按照套路拆点,每个点内有权值。

查看代码
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
#include <cstdio>
#include <map>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
const int N = 210, M = 2e4 + 10, inf = 100;
bool vis[N];
string name[N];
int n, m, st, ed, d[N], cur[N];
int idx = -1, hd[N], nxt[M], edg[M], wt[M], cst[M];
void add(int a, int b, int c, int d)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
cst[idx] = d;
}
bool bfs()
{
for (int i = st; i <= ed; i++)
d[i] = -inf;
d[st] = 0;
cur[st] = hd[st];
queue<int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
vis[t] = false;
for (int i = hd[t]; ~i; i = nxt[i])
if (d[t] + cst[i] > d[edg[i]] && wt[i])
{
d[edg[i]] = d[t] + cst[i];
cur[edg[i]] = hd[edg[i]];
if (!vis[edg[i]])
{
q.push(edg[i]);
vis[edg[i]] = true;
}
}
}
return d[ed] != -inf;
}
int exploit(int x, int limit)
{
if (x == ed)
return limit;
int res = 0;
vis[x] = true;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + cst[i] && wt[i] && !vis[edg[i]])
{
int t = exploit(edg[i], min(limit - res, wt[i]));
if (!t)
d[edg[i]] = -inf;
res += t;
wt[i] -= t;
wt[i ^ 1] += t;
}
}
vis[x] = false;
return res;
}
void dinic(int &flow, int &cost)
{
int f;
cost = 0, flow = 0;
while (bfs())
while (f = exploit(st, inf))
flow += f, cost += f * d[ed];
}
int main()
{
cin >> n >> m;
st = 0, ed = n << 1 | 1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
map<string, int> f;
for (int i = 1; i <= n; i++)
{
cin >> name[i];
f[name[i]] = i;
}
add(st, 1, 2, 0);
add(1, st, 0, 0);
add(n, ed, 2, 0);
add(ed, n, 0, 0);
add(1, 1 + n, 2, 0);
add(1 + n, 1, 0, 0);
add(n, n + n, 2, 0);
add(n + n, n, 0, 0);
for (int i = 2; i < n; i++)
{
add(i, i + n, 1, 1);
add(i + n, i, 0, -1);
}
for (string a, b; m; m--)
{
cin >> a >> b;
int x = f[a], y = f[b];
if (x > y)
swap(x, y);
add(x + n, y, n, 0);
add(y, x + n, 0, 0);
}
int flow, cost;
dinic(flow, cost);
if (flow < 2)
{
cout << "No Solution!";
return 0;
}
cout << cost + 2 << endl;
vector<int> ans;
int p = 1 + n;
while (p != n + n)
{
ans.push_back(p - n);
for (int i = hd[p]; ~i; i = nxt[i])
if (edg[i] > 0 && edg[i] <= n && wt[i] < n && !vis[edg[i]])
{
vis[edg[i]] = true;
p = edg[i] + n;
break;
}
}
p = n;
while (p != 1)
{
ans.push_back(p);
for (int i = hd[p]; ~i; i = nxt[i])
if (edg[i] > n && edg[i] < ed && wt[i] && !vis[edg[i] - n])
{
vis[edg[i] - n] = true;
p = edg[i] - n;
break;
}
}
for (int i : ans)
cout << name[i] << endl;
cout << name[1];
return 0;
}