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 ...
随机推荐
- 更改 MATLAB 默认工作路径
步骤: 1. 以管理员身份打开记事本,然后打开 MATLAB安装路径\MATLAB\R2010b\toolbox\local\matlabrc.m 文件,即打开安装路径下的 matlabrc.m 文件 ...
- 1185: 零起点学算法92——单词数(C)
一.题目 http://acm.wust.edu.cn/problem.php?id=1185&soj=0 二.分析 统计的是不同的单词数,即重复的单词只统计一次: 多组输入: 每行不超过10 ...
- java 随机生成4位随机数
java 随机生成4位的随机数测试类 @org.junit.Testpublic void testRandom(){ String msg="您的注册码为%s,谢谢注册!"; S ...
- SQL Server邮件标识点
<br>---换行  :---空格 <H1></H1>---标题 --定义表格格式 N'<table border="1" ...
- RabbitMQ 应用一
(百度百科)MQ全称为Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的 ...
- 【转载】 C#中日期类型DateTime的日期加减操作
在C#开发过程中,DateTime数据类型用于表示日期类型,可以通过DateTime.Now获取当前服务器时间,同时日期也可以像数字一样进行加减操作,如AddDay方法可以对日期进行加减几天的操作,A ...
- 用ajax和PHP实现简单的流程管理
首先要先有一个新建流程的页面xinjian.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- laravel管理模型插入
post控制器public function comment(Post $post,Request $request){ try{ if(empty($request->content)){ E ...
- Android笔记(十五) Android中的基本组件——单选框和复选框
单选框和多选框通常用来在设置用户个人资料时候,选择性别.爱好等,不需要用户直接输入,直接在备选选项中选择,简单方便. 直接看代码: <?xml version="1.0" e ...
- JAVA工程师必学技能,进阶&涨薪的推进器!这份实战教程请收下
Netty 作为互联网中间件的基石,是 JAVA 工程师进阶为高级程序员必备的能力之一.也是目前是互联网中间件领域使用最广泛最核心的网络通信框架. Netty是一个高性能.异步事件驱动的NIO框架,它 ...