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. python 之 数据库(创建表的完整语法、基本数据类型)

    10.4 创建表的完整语法 create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); #类型:使用限 ...

  2. dg搭建后oracle_redo不存在

    目的:在oracle 10.2.0.4 环境中,搭建oracle dg遇到 备库redo不存在的问题,另一位同事搭建oracle 11.2.0.4 dg在备库也遇到同样的问题,如下描述处理过程. 参考 ...

  3. metasploit情报收集

    1.msf连接数据库 service postgresql start(postgresql默认用户名scott,密码tiger) msf > db_connect 用户名:密码@127.0.0 ...

  4. 一、ribbon如何集成在openfeign中使用

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 ribbon是springcloud封装的一个基于http客户端负载均衡的组件.spri ...

  5. 【转】Java最常见的200+面试题

    今天看到一份面试题总结,感觉很到位,主要包括以下模块:Java基础.容器.多线程.反射.对象拷贝.Java Web模块,异常.网络.设计模式.Spring/Spring MVC .Spring Boo ...

  6. ArcGIS Runtime SDK for Android 定位权限(GPS定位\网络定位)

    ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION: android.permission.ACCESS_COARSE_LOCATION:是基站定位,即基于无线网络 ...

  7. 2019.9.27,SAP成都研究院数字创新空间团队建设,射箭和游泳

    2019年9月27日,秋高气爽,SAP成都研究院数字创新团队全体成员又迎来了一次团队建设活动.这次的主题是:射箭. 在正式活动之前,大家先享用了一顿泰式海鲜火锅: 吃饱喝足之后,我们来到了名为&quo ...

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

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

  9. 实现对MySQL数据库进行分库/分表备份(shell脚本)

    工作中,往往数据库备份是件非常重要的事情,毕竟数据就是金钱,就是生命!废话不多,下面介绍一下:如何实现对MySQL数据库进行分库备份(shell脚本) Mysq数据库dump备份/还原语法: mysq ...

  10. Vue框架之vuex的使用

    1.首先需要在你的项目目录下安装vuex 终端命令: 2.在全局组件中导入与声明vuex 3.创建store实例对象 let store = new Vuex.store({ state:{ }, m ...