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 ...
随机推荐
- Web服务器和Tomcat
Web服务器常用: WebLogic:是BEA公司的推出的产品,现在已经被oracle收购,是目前应用最广泛的Web服务器,支持JavaEE规范,商用收费,开发者可以免费使用. WebSphere:I ...
- Linux大道——博客目录
Linux基础 第一章 计算机基础 计算机基础 网络基础 第二章 Linux基础
- 利用Python进行数据分析 第7章 数据清洗和准备(2)
7.3 字符串操作 pandas加强了Python的字符串和文本处理功能,使得能够对整组数据应用字符串表达式和正则表达式,且能够处理烦人的缺失数据. 7.3.1 字符串对象方法 对于许多字符串处理和脚 ...
- ReflectionTest:由输入的类名得到类的信息
package reflection; import java.lang.reflect.*; import java.util.*; public class ReflectionTest { pu ...
- linux 命令行 光标移动技巧等
看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下面工作.但是事情也不是那么简单,还是需要知道“如何去做” ...
- 转载:centos安装gitlab详解
原文地址:http://blog.csdn.net/jiangtao_st/article/details/73612298 一, 服务器快速搭建gitlab方法 可以参考gitlab中文社区 的教程 ...
- Windows cmd操作文件夹
ir // 列出目录下所有文件夹 rd dirname // 删除dirname文件夹(空文件夹) rd /s/q dirname // 删除dirname文件夹(非空)
- ABAP Code Inspector那些隐藏的功能,您都知道吗?
最近有粉丝在后台给我留言,说新知识太多,"学不动了".所谓温故而知新,今天我们就来重温下ABAP里的Code Inspector的用法. 2015年6月,我在SAP社区上写了一篇博 ...
- JMeter测试clickhouse
使用JMeter对clickhouse连接测试 1.测试计划 jmeter通过JDBC连接数据库需要先引入对应的驱动包,驱动包的版本要与服务器数据库版本一致,我用的驱动版本是:clickhouse-j ...
- Java上传图片到服务器
HTML页面的标签 <div id="div_selectpic" align="right" style="width: 300px;font ...