icpc 银川 H. Delivery Route SPFA优化
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优化的更多相关文章
- 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...
- 2019ICPC(银川) - Delivery Route(强连通分量 + 拓扑排序 + dijkstra)
Delivery Route 题目:有n个派送点,x条双向边,y条单向边,出发点是s,双向边的权值均为正,单向边的权值可以为负数,对于单向边给出了一个限制:如果u->v成立,则v->u一定 ...
- 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 ...
- 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- Codeforces 938D Buy a Ticket 【spfa优化】
用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是 ...
- bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】
洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...
- h.264 率失真优化
Rate Distortion Optimization 搜索时,一个不可避免的问题就是如何对mv进行比较,从而得到最优 对于同一压缩算法来说,码率越高表示图像质量越好.失真越小,但是码率越高要求更大 ...
- spfa优化板子
用双端队列,当待加入元素小于队首元素时 加入队首, 否则 加入队尾 slf优化 if(!vis[e.v]) { if(Q.empty()) Q.push_front(e.v); else { if(d ...
随机推荐
- PB笔记之取项次最大值(即使用.describe(" evaluate('ITM_max',0) ") 获取列的最大值) 的条件
dw_1.describe(" evaluate('ITM_max',0) ") :使用 describe 配合 evaluate 取列的最大最小值(或其它表达式)时,必须在数据 ...
- stm32f103的HSI设置
HSI基本知识 HSI是8MRC震荡电路,精度1%. PLL的设置必须在其被激活前完成,输出必须被设置温48M或者72M LSE:通过在备份域控制寄存器(RCC_BDCR)里的LSEON位启动和关闭. ...
- Git config 使用说明(转)
原文:https://blog.csdn.net/gdutxiaoxu/article/details/79253737
- RMAN备份,catalog注册rman带库备份信息
客户需求:测试恢复的过程中,控制文件是全备时期的,recover database无法恢复到指定日期,控制文件中缺失后续新的归档备份信息. 方法:1.控制文件rman注册后续带库中的归档备份: 2.使 ...
- VsCode开发Angular的必备插件
1 概述 一般个人开发或者小公司开发都会使用破解版软件,除非比较尊重正版且不太缺钱的人才会用正版,但是大型公司有严格的规定,不允许员工使用盗版软件. 这时候我就不得不从WebStorm转向VsCode ...
- Windows中的库编程
Windows操作系统中,库分为动态链接库(dll)和静态链接库(lib) 动态库是Windows中实现代码共享的一种方式.它是一个二进制式文件,不可单独运行,需要调用方调用才能运行.在Windows ...
- 如何录屏做GIF图
网上找了一下,ScreenToGif 这个神器 https://github.com/NickeManarin/ScreenToGif https://github.com/NickeManarin/ ...
- 用.net4中的DynamicObject实现简单AOP
public class DynamicWrapper : DynamicObject { private readonly object source; public DynamicWrapper( ...
- POJ1611(The Suspects)--简单并查集
题目在这里 关于SARS病毒传染的问题.在同一个组的学生是接触很近的,后面也会有新的同学的加入.其中有一位同学感染SARS,那么该组的所有同学得了SARS.要计算出有多少位学生感染SARS了.编号为0 ...
- MD 使用 i5ting_toc 转换成 HTML
MD 使用 i5ting_toc 转换成 HTML 本文作者:天析 作者邮箱:2200475850@qq.com 发布时间: Wed, 10 Jul 2019 13:59:00 +0800 前言 md ...