[CF241E]Flights

题目大意:

给一张\(n(n\le1000)\)个点\(m(m\le5000)\)条边的DAG,确定每条边的边权\(w_i(w_i\in\{1,2\})\),使得所有从\(1\)到\(n\)的路径拥有相同的长度。

思路:

首先用BFS求出所有与\(1\)到\(n\)路径有关的点构成的子图。

这样,\(1\)到子图上每个点的长度都是确定的,即终点相同的路径拥有相同的长度。有\(d_i\)表示点\(1\)到点\(i\)的距离,则对于边\(u\to v\),有\(1\le d_v-d_u\le2\)。这样我们就可以得到一个差分约束系统,使用SPFA求最长路即可。

源代码:

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1001,M=5000;
struct Edge {
int u,v;
};
Edge edge[M];
std::vector<int> e[N];
std::vector<Edge> h[N];
int n,m,vis[N],dis[N];
bool f[N],g[N],inq[N];
std::queue<int> q;
inline void bfs1() {
for(register int i=0;i<m;i++) {
e[edge[i].u].push_back(edge[i].v);
}
q.push(1);
f[1]=true;
while(!q.empty()) {
const int &x=q.front();
for(auto &y:e[x]) {
if(!f[y]) {
q.push(y);
f[y]=true;
}
}
q.pop();
}
for(register int i=1;i<=n;i++) {
e[i].clear();
}
}
inline void bfs2() {
for(register int i=0;i<m;i++) {
e[edge[i].v].push_back(edge[i].u);
}
q.push(n);
g[n]=true;
while(!q.empty()) {
const int &x=q.front();
for(auto &y:e[x]) {
if(!g[y]) {
q.push(y);
g[y]=true;
}
}
q.pop();
}
for(register int i=1;i<=n;i++) {
e[i].clear();
}
}
inline void spfa() {
q.push(1);
while(!q.empty()) {
const int &x=q.front();
for(auto &j:h[x]) {
const int &y=j.u,&w=j.v;
if(dis[x]+w>dis[y]) {
dis[y]=dis[x]+w;
if(!inq[y]) {
inq[y]=true;
q.push(y);
if(++vis[y]>n) {
throw 0;
}
}
}
}
q.pop();
inq[x]=false;
}
}
int main() {
n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint();
edge[i]=(Edge){u,v};
}
bfs1();
bfs2();
for(register int i=0;i<m;i++) {
const int &u=edge[i].u,&v=edge[i].v;
if(f[u]&&g[u]&&f[v]&&g[v]) {
h[u].push_back((Edge){v,1});
h[v].push_back((Edge){u,-2});
}
}
try {
spfa();
} catch(...) {
puts("No");
return 0;
}
puts("Yes");
for(register int i=0;i<m;i++) {
const int &u=edge[i].u,&v=edge[i].v;
if(f[u]&&g[u]&&f[v]&&g[v]) {
printf("%d\n",dis[v]-dis[u]);
} else {
puts("1");
}
}
return 0;
}

[CF241E]Flights的更多相关文章

  1. CF241E Flights 题解

    题目 做了一下这道题,突然发现自己忘了差分约束,赶紧复习一下. 设当前有n个变量 a1,a2,...,an ,有若干组限制形如 ai≤aj+k (其中k为常数),则由点j向点i连一条边权为k的边,再从 ...

  2. CF241E Flights 差分约束

    传送门 差分约束永远是Itst最烂的图论知识点没有之一qwq 先用dfs把在\(1\)到\(N\)的路径上的所有点都拿出来,其他的点和边状态任意都不会影响答案. 然后考虑设\(dis_i\)表示从\( ...

  3. 题解 CF241E Flights

    题目传送门 题目大意 给出一个 \(n\) 个点 \(m\) 条边的 \(\texttt{DAG}\) ,给每条边设定边权为 \(1\) 或者 \(2\) ,使得 \(1\to n\) 的每条路径长度 ...

  4. 【CF241E】Flights(差分约束)

    [CF241E]Flights(差分约束) 题面 CF 有\(n\)个点\(m\)条边,要求给每条边赋一个\(1\)或\(2\)的边权,判断能否使得每一条\(1\)到\(n\)的路径的权值和都相等,如 ...

  5. 【CF241E】Flights

    [CF241E]Flights 题面 洛谷 题解 对于原来的图,如果一条边不出现在\(1\)到\(n\)的路径上面,直接\(ban\)掉即可. 那么考虑一条边\(u\rightarrow v\),一定 ...

  6. 「CF241E」Flights

    传送门 Luogu 解题思路 首先对于所有不属于任何一条路径上的边,它的权值是任意的. 对于所有在路径上的边 \((u,v)\) 满足 \(1\le dis_v-dis_u\le2\) 差分约束即可. ...

  7. Codeforces Round #384 (Div. 2) A. Vladik and flights 水题

    A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...

  8. (中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。

    In the country there are exactly n cities numbered with positive integers from 1 to n. In each city ...

  9. [Swift]LeetCode787. K 站中转内最便宜的航班 | Cheapest Flights Within K Stops

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

随机推荐

  1. Linux下clock计时函数学习

    平时在Linux和Winows下都有编码的时候,移植代码的时候免不了发现一些问题.1. 你到底准不准?关于clock()计时函数首先是一段简单的测试代码,功能为测试从文本文件读取数据并赋值给向量最后打 ...

  2. 配置samba文件服务器

    1.打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--> ...

  3. 查看tomcat运行状态

    实时查看tomcat并发连接数: netstat -na | grep ESTAB | grep 8080 | wc -l 实时查看apache并发连接数: netstat -na | grep ES ...

  4. 织梦dedeCMS数据库结构字段说明-简略说明

    dede_addonarticle 附加文章表 aid int(11) 文章编号typeid int(11) 分类栏目编号body mediumtext 文章内容dede_addonflash 附加F ...

  5. zabbix系列(八)zabbix添加对web页面url的状态监控

    通过zabbi做web监控不仅仅可以监控到站点的响应时间,还可以根据站点返回的状态码,或者响应时间做报警 1.对需要监控的主机添加web监控   在configuration—hosts 中打开主机列 ...

  6. MyBatis返回Map键值对数据

    List<Map<String, String>> getMtypeList(); <select id="getMtypeList" resultT ...

  7. MyEclipse中如何配置默认jsp为UTF-8格式

  8. S5PV210初始化系统时钟

    S5PV210初始化系统时钟 S5PV210时钟体系S5PV210中包含3大类时钟domain,分别是主系统时钟domain (简称MSYS,下面将使用简称来进行相关讲解).显示相关的时钟domain ...

  9. MSF初体验—入侵安卓手机

    1.生成apk程序 msfvenom -p android/meterpreter/reverse_tcp LHOST=192.168.1.101 LPORT=5555 R > apk.apk ...

  10. cf1061D 贪心+multiset 好题!

    cf上的思维题真好! 本题是在模拟的基础上贪心即可:将n段时间按照左端点(右端点为第二关键字)从小到大排序,然后遍历每一个时间段. 对于每一个时间段[li,ri],先找到multiset中最靠近li但 ...