[网络流24题]P2770 航空路线
作者:互联网
拆点,费用流,需要输出方案,需要思考一下的建模
https://www.luogu.com.cn/problem/P2770]
题意
给一串城市,从东到西排开,让你找一条路径使得可以从最东边的起点走到最西边然后再回来,并且除了起点,不会重复经过同一个城市。
Tutorial
走过去再走回来,可以将其看成两条不相交的从东到西的路径,然后就十分简单了。除了起点流量为2外,其他拆点流量为1
点击查看代码
#include <bits/stdc++.h>
#define endl '\n'
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define P pair<int, int>
typedef long long ll;
using namespace std;
const int maxn = 100 * 2 + 5;
const ll inf = 1e18;
int n, cnt_edge = 1, S, T;
int a1[maxn], a2[maxn], head[maxn];
ll dis[maxn];
bool vis[maxn];
struct edge {
int to, nxt;
ll flow, cost;
} e[(maxn * maxn) << 2];
inline void add(int u, int v, ll w, ll c) {
// cout << "add: " << u << " " << v << " " << w << endl;
e[++cnt_edge].nxt = head[u];
head[u] = cnt_edge;
e[cnt_edge].to = v;
e[cnt_edge].flow = w;
e[cnt_edge].cost = c;
}
/*u,v,flow,cost*/
inline void addflow(int u, int v, ll w, ll c) {
add(u, v, w, c);
add(v, u, 0, -c);
}
inline bool spfa(int on) {
memset(vis, 0, sizeof(vis));
if (on == 1)
for (int i = 0; i <= T; i++) dis[i] = inf;
else
for (int i = 0; i <= T; i++) dis[i] = -inf;
queue<int> q;
q.push(S);
dis[S] = 0;
vis[S] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
// cout << "->" << y << endl;
if ((on == 1 && e[i].flow && dis[y] > dis[x] + e[i].cost) ||
(on == -1 && e[i].flow && dis[y] < dis[x] + e[i].cost)) {
dis[y] = dis[x] + e[i].cost;
if (!vis[y]) q.push(y), vis[y] = 1;
}
}
}
if (on == 1)
return dis[T] != inf;
else
return dis[T] != -inf;
}
ll dfs(int x, ll lim) {
vis[x] = 1;
if (x == T || lim <= 0) return lim;
ll res = lim;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (dis[y] != dis[x] + e[i].cost || e[i].flow <= 0 || vis[y]) continue;
ll tmp = dfs(y, min(res, e[i].flow));
res -= tmp;
e[i].flow -= tmp;
e[i ^ 1].flow += tmp;
if (res <= 0) break;
}
return lim - res;
}
/*on == 1时为最小费用,on == -1为最大费用*/
inline void Dinic(int on, ll& a, ll& b) {
ll res = 0, cost = 0;
while (spfa(on)) {
ll flow = dfs(S, inf);
res += flow, cost += flow * dis[T];
}
// return cost;
a = res, b = cost;
}
int id(int x, int day) { return day * n + x; }
unordered_map<string, int> table;
string name[maxn];
bool flag = 0;
vector<string> ans;
void dfs_path(int u) {
// cout << "->" << u << endl;
if (flag) return;
vis[u] = 1;
if (1 <= u && u <= n) {
ans.push_back(name[u]);
// cout << u << " " << name[u] << "\n";
}
if (u == T) {
flag = 1;
return;
// cout << name[1]<<endl;
// exit(0);
}
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
// cout << v << endl;
int nowid = u > n ? u - n : u;
int nexid = v > n ? v - n : v;
if (e[i ^ 1].flow == 0 || flag || vis[v] || nexid < nowid) continue;
// cout << u << " " << v << endl;
// // cout << ">" << i << " " << u << " " << e[i].to << " " << e[i + 1].to
// // << " " << e[i + 1].flow << endl;
// // cout<<">" << (i ^ 1) << " " << e[i ^ 1].to << " " << u << " "
// // << e[i^1].flow << endl;
e[i].flow++;
e[i ^ 1].flow--;
dfs_path(v);
if (flag) return;
}
}
int main() {
int m;
cin >> n >> m;
S = n * 2 + 5, T = S + 1;
addflow(S, id(1, 0), 2, 1);
addflow(id(n, 1), T, 2, 1);
for (int i = 1; i <= n; i++) {
cin >> name[i];
table[name[i]] = i;
if (i == 1 || i == n)
addflow(id(i, 0), id(i, 1), 2, 0);
else
addflow(id(i, 0), id(i, 1), 1, 0);
}
string uu, vv;
int u, v;
for (int i = 1; i <= m; i++) {
cin >> uu >> vv;
u = table[uu], v = table[vv];
if (u > v) swap(u, v);
// cout << u << " " << v << endl;
addflow(id(u, 1), id(v, 0), inf, 1);
}
ll cnt, cost;
Dinic(-1, cnt, cost);
// cout << cnt << " " << cost << endl;
if (cnt <= 1) {
cout << "No Solution!" << endl;
return 0;
}
cout << cost - 4 << endl;
memset(vis, 0, sizeof vis);
dfs_path(id(1, 0));
for (auto i : ans) cout << i << endl;
flag = 0;
memset(vis, 0, sizeof vis);
ans.clear();
dfs_path(id(1, 0));
for (int i = ans.size() - 2; ~i; i--) {
cout << ans[i] << endl;
}
return 0;
}
标签:24,int,ll,路线,vis,maxn,P2770,id,dis 来源: https://www.cnblogs.com/FushimiYuki/p/15572162.html