Problem Description

Pony is the boss of a courier company. The company needs to deliver packages to n offices numbered from 1 to n. Especially, the s-th office is the transfer station of the courier company. There are x ordinary two-way roads and y one-way roads between these offices. The delivery vans will consume ci power if they pass through the i-th road. In general, the power consumption on one road must be non-negative. However, thanks to the experimental charging rail, the consumption may be negative on some one-way roads. Besides, Pony got the following public information. The relevant department promised that if there is a one-way street from ai to bi, it is impossible to return from bi to ai. To avoid the delivery vans anchoring on the road, Xiaodao wants to find these lowest power consumptions from the transfer station to these offices

Input

The first line contains four integers n (1 ≤ n ≤ 25000); x; y (1 ≤ x; y ≤ 50000), and s (1 ≤ s ≤ n). This is followed by x + y lines, each line of which contains three integer ai; bi (1 ≤ ai; bi ≤ n; ai 6= bi) and ci (−10000 ≤ ci ≤ 10000) describing the roads. The first x given roads are ordinary two-way roads, and the last y given roads are one-way roads.

Output

The output should contain n lines, the i-th line represents the minimum energy consumption from s-th to the i-th office if possible, or output “NO PATH” if it is impossible to reach the i-th office.

Sample Input

6 3 3 4

1 2 5

3 4 5

5 6 10

3 5 -100

4 6 -100

1 3 -10

Sample Output

NO PATH

NO PATH

50

-95

-100

Analysis of ideas

n个点,x条双向边,y条单向边(有可能负权),s是起点,求s到各个点的最短距离,每个点最多被访问一次

手写双端队列,并且使用vector一直卡在最后一个数据,然后就用了链式前向星

spfa slf+容错

Accepted code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 200100;
int n,m,k,t; inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
} ll dis[maxn];
bool vis[maxn];
ll val = 0;
int head[maxn], to[maxn], w[maxn], nex[maxn], e = 1; void add(int a,int b,int c)
{
to[e] = b;
nex[e] = head[a];
head[a] = e;
w[e] = c;
e++;
} int q[20000000];
void spfa(int s)
{
for(int i = 1;i <= n; i++) dis[i] = 1e18;
dis[s] = 0; int l = 1e7,r = 1e7; //l是队首,r是队尾
q[l] = s; while(l <= r)
{
s = q[l]; l++;
vis[s] = 0; //标记出队 for(int i = head[s]; i ; i = nex[i])
{
int v = to[i];
if(dis[v] > dis[s]+w[i])
{
dis[v] = dis[s]+w[i];
if(!vis[v])
{
vis[v] = 1;
if(l > r) q[++r] = v;
else
{
if(dis[v] < dis[q[l]]+val) q[--l] = v;
else q[++r] = v;
} }
}
}
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int x,y,s,u,v,w;
n = read(),x = read(), y = read(),s = read();
for(int i = 0; i < x; i++)
{
u = read(),v = read(), w = read();
add(u,v,w);
add(v,u,w);
val += 2*w;
}
for(int i = 0; i < y; i++)
{
u = read(),v = read(), w = read();
add(u,v,w);
val += w;
}
val = sqrt(val)/100;
spfa(s);
for(int i = 1; i <= n; i++)
{
if(dis[i] == 1e18)
{
puts("NO PATH");
}
else printf("%lld\n",dis[i]);
}
return 0;
}

参考博客:

https://blog.csdn.net/guogai13/article/details/103328697

icpc 银川 H. Delivery Route SPFA优化的更多相关文章

  1. 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...

  2. 2019ICPC(银川) - Delivery Route(强连通分量 + 拓扑排序 + dijkstra)

    Delivery Route 题目:有n个派送点,x条双向边,y条单向边,出发点是s,双向边的权值均为正,单向边的权值可以为负数,对于单向边给出了一个限制:如果u->v成立,则v->u一定 ...

  3. 2019 ICPC 银川网络赛 H. Fight Against Monsters

    It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Eg ...

  4. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  5. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  6. Codeforces 938D Buy a Ticket 【spfa优化】

    用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是 ...

  7. bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】

    洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...

  8. h.264 率失真优化

    Rate Distortion Optimization 搜索时,一个不可避免的问题就是如何对mv进行比较,从而得到最优 对于同一压缩算法来说,码率越高表示图像质量越好.失真越小,但是码率越高要求更大 ...

  9. spfa优化板子

    用双端队列,当待加入元素小于队首元素时 加入队首, 否则 加入队尾 slf优化 if(!vis[e.v]) { if(Q.empty()) Q.push_front(e.v); else { if(d ...

随机推荐

  1. (三)spring Security 从数据库中检索用户名和密码

    文章目录 配置 Druid 数据源 数据库 Mapper 文件 自定义 `UserDetailsService` 自定义登陆校验器 `AuthenticationProvider ` 配置 secur ...

  2. 如何升级centos7 内核方法

    关于内核说明: 版本性质:主分支ml(mainline),稳定版(stable),长期维护lt(longterm) 版本命名格式:“A.B.C" A代表内核版本号 B代表内核主版本号 C代表 ...

  3. VMWare安装Ubuntu16.04

    一 概述 VMware Workstation 12的安装(略过,自行百度) Ubuntu16.04的安装 克隆出多个镜像 二 Ubuntu16.04的安装 1 准备 Window10 专业版(关闭H ...

  4. github新建远程仓库初始化记录

    …or create a new repository on the command line echo "# 输出内容" >> README.md git init ...

  5. Django Rest framework实现流程

    目录 一 什么是restful架构 二 Django REST framework简介 三 Django REST framework原理 四 Django REST framework源码流程 五 ...

  6. 【转载】腾讯云安全组如何开放3306端口让Mysql可访问

    Mysql数据库的默认端口号为3306,在服务器安装好Mysql数据库后,如果使用的服务器是阿里云或者腾讯云服务器,如果在后台启用了安全组功能,则需要在安全组中对3306端口的入站规则进行放行,只有在 ...

  7. [技术翻译]您应该知道的13个有用的JavaScript数组技巧

    本次预计翻译三篇文章如下: 01.[译]9个可以让你在2020年成为前端专家的项目 02.[译]预加载响应式图像,从Chrome 73开始实现 03.[译]您应该知道的13个有用的JavaScript ...

  8. python识别文字tesseract

    Ubuntu版本: .tesseract-ocr安装 sudo apt-get install tesseract-ocr .pytesseract安装 sudo pip install pytess ...

  9. Vue指令之`v-bind`的三种用法及v-on事件指令

    v-bind:是 Vue中,提供的用于绑定属性的指令 1. 直接使用指令`v-bind` 2. 使用简化指令`:` 3. 在绑定的时候,拼接绑定内容:`:title="btnTitle + ...

  10. 【leetcode】266. Palindrome Permutation

    原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...