「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\)
「CF241E」Flights的更多相关文章
- 「CF576D」 Flights for Regular Customers
「CF576D」 Flights for Regular Customers 对不起我又想网络流去了 你看这长得多像啊,走过至少多少条边就是流量下界,然后没上界 但是这个题求的最少走多少条边啊...完 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
随机推荐
- 解决Creating Server TCP listening socket 54.179.160.162:7001: bind: Cannot assign requested address
背景:之前在测试环境搭过一个redis集群,运维把服务器重启之后我重新开启redis集群始终起不来,但是有没有任何日志,经过如下步骤最终解决问题 1.修改日志路径,根据日志查看为什么会启动失败[前期操 ...
- 配置数据库属性validationQuery
配置数据库时,属性validationQuery默认值为“select 1”,对于oracle值应为“select 1 from dual” validationQuery属性:用来验证数据库连接的语 ...
- 用OpenCV读取摄像头
首先插入摄像头 在电脑中查看 #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp ...
- AcWing 154. 滑动窗口
https://www.acwing.com/problem/content/156/ #include <iostream> using namespace std; ; int a[N ...
- LED Decorative Light Manufacturer - Led Wall Lamp Performance Characteristics
LED Decorative Light Manufacturer introduction: LED wall lamp is a light-emitting diode as a ligh ...
- unittest的discover方法
转载:https://www.cnblogs.com/imyalost/p/9048386.html discover()方法 discover(start_dir, pattern ='test * ...
- flutter web 配置环境及运行(windows)
此下 操作 都是基于 windows 一, 将镜像添加到 用户环境变量中 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用 ...
- JS-对象常用方法整理
查看对象的方法,继续控制台输出,如图: hasOwnProperty():返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键). let object1 = new Obje ...
- 8.10-Day1T1-数字(number)
数字number 题目大意 给定n,k,s,从1到n中取出k个数,使其之和等于s 求可行的方案数(模1e9+7) 题解 一眼dp,于是我去写了dfs,带着少的可怜的剪枝,快乐的tle着... 设 f[ ...
- Git - 03. git 工作空间
1. 概述 git 存放代码的地方 2. 创建 命令 # 1. 从无到有 > git init # 2. 从远程拉去现有的仓库 > git clone <url> 3. 文件生 ...