「CF241E」Flights
传送门
Luogu
解题思路
首先对于所有不属于任何一条路径上的边,它的权值是任意的。
对于所有在路径上的边 \((u,v)\) 满足 \(1\le dis_v-dis_u\le2\)
差分约束即可。
细节注意事项
- 用dfs判负环时注意一下时间效率
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 1010;
const int __ = 5010 * 2 + 1010;
int n, m, vis[_], dis[_], exi[_];
struct edge{ int x, y; }g[__];
vector < int > G1[_], G2[_];
int tot, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
inline int check(int i) { return vis[g[i].x] == 3 && vis[g[i].y] == 3; }
inline int spfa(int u) {
exi[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[v] < dis[u] + w[i]) {
dis[v] = dis[u] + w[i];
if (exi[v]) return 0;
if (!spfa(v)) return 0;
}
}
exi[u] = 0;
return 1;
}
inline void dfs1(int u) { vis[u] |= 1; for (rg int v : G1[u]) if (!(vis[v] & 1)) dfs1(v); }
inline void dfs2(int u) { vis[u] |= 2; for (rg int v : G2[u]) if (!(vis[v] & 2)) dfs2(v); }
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int u, v, i = 1; i <= m; ++i)
read(u), read(v), G1[u].push_back(v), G2[v].push_back(u), g[i] = (edge) { u, v };
dfs1(1), dfs2(n);
for (rg int i = 1; i <= m; ++i)
if (check(i)) Add_edge(g[i].x, g[i].y, 1), Add_edge(g[i].y, g[i].x, -2);
for (rg int i = 1; i <= n; ++i) Add_edge(0, i, 0), dis[i] = -0x3f3f3f3f;
if (!spfa(0)) return puts("No"), 0;
puts("Yes");
for (rg int i = 1; i <= m; ++i) {
if (check(i)) printf("%d\n", dis[g[i].y] - dis[g[i].x]);
else puts("1");
}
return 0;
}
完结撒花 \(qwq\)