Description

Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查。他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T。这些城镇之间通过R条道路 (1 <= R <= 50,000,编号为1到R) 和P条航线 (1 <= P <= 50,000,编号为1到P) 连接。每条道路i或者航线i连接城镇A_i (1 <= A_i <= T)到B_i (1 <= B_i <= T),花费为C_i。对于道路,0 <= C_i <= 10,000;然而航线的花费很神奇,花费C_i可能是负数(-10,000 <= C_i <= 10,000)。道路是双向的,可以从A_i到B_i,也可以从B_i到A_i,花费都是C_i。然而航线与之不同,只可以从A_i到B_i。事实上,由于最近恐怖主义太嚣张,为了社会和谐,出台 了一些政策保证:如果有一条航线可以从A_i到B_i,那么保证不可能通过一些道路和航线从B_i回到A_i。由于FJ的奶牛世界公认十分给力,他需要运送奶牛到每一个城镇。他想找到从发送中心城镇S(1 <= S <= T) 把奶牛送到每个城镇的最便宜的方案,或者知道这是不可能的。

Input

* 第1行:四个空格隔开的整数: T, R, P, and S * 第2到R+1行:三个空格隔开的整数(表示一条道路):A_i, B_i 和 C_i * 第R+2到R+P+1行:三个空格隔开的整数(表示一条航线):A_i, B_i 和 C_i

Output

* 第1到T行:从S到达城镇i的最小花费,如果不存在输出"NO PATH"。

Sample Input

6 3 3 4
1 2 5
3 4 5
5 6 10
3 5 -100
4 6 -100
1 3 -10

样例输入解释:

一共六个城镇。在1-2,3-4,5-6之间有道路,花费分别是5,5,10。同时有三条航线:3->5,
4->6和1->3,花费分别是-100,-100,-10。FJ的中心城镇在城镇4。

Sample Output

NO PATH
NO PATH
5
0
-95
-100

样例输出解释:

FJ的奶牛从4号城镇开始,可以通过道路到达3号城镇。然后他们会通过航线达到5和6号城镇。
但是不可能到达1和2号城镇。

Solution

做法:spfa+SLF优化

裸上$spfa$会$T$,加个堆优化也$T$了,所以就$SLF$咯

$Dijkstra$也可以写但是好麻烦...

写$Dijkstra$的话要缩点,还要拓扑...所以直接上$spfa+SLF$就好啦

#include <bits/stdc++.h>

using namespace std ;

#define N 200010
const int inf = 1e10 ; inline void read( int &x ) {
x = ; int f = ; char c = getchar() ;
while( c < '' || c > '' ) { if( c == '-' ) f = -f ; c = getchar() ; }
while( c >= '' && c <= '' ) { x = (x<<) + (x<<) + c - ; c =getchar() ; }
x = x * f ;
} int n , m1, m2 , s ;
int head[ N ] , cnt ;
int d[ N ] , vis[ N ] ;
struct edge {
int to , nxt ,v ;
}e[ N ] ; deque < int > q ; void ins( int u , int v , int w ) {
e[ ++ cnt ].to = v ;
e[ cnt ].nxt = head[ u ] ;
e[ cnt ].v = w ;
head[ u ] = cnt ;
} void spfa() {
vis[ s ] = ;
for( int i = ; i <= n ; i ++ ) d[ i ] = inf ;
d[ s ] = ;
q.push_back( s ) ;
while( !q.empty() ) {
int u = q.front() ;
q.pop_front() ;
vis[ u ] = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
int v = e[ i ].to ;
if( d[ v ] > d[ u ] + e[ i ].v ) {
d[ v ] = d[ u ] + e[ i ].v ;
if( ! vis[ v ] ) {
vis[ v ] = ;
if( !q.empty() && d[ v ] >= d[ q.front() ] ) q.push_back( v ) ;
else q.push_front( v ) ;
}
}
}
}
} int main() {
read( n ) ; read( m1 ) ; read( m2 ) ; read( s ) ;
for( int i = ; i <= m1 ; i ++ ) {
int u , v , w ;
read( u ) ; read( v ) ; read( w ) ;
ins( u , v , w ) ;
ins( v , u , w ) ;
}
for( int i = ; i <= m2 ; i ++ ) {
int u , v , w ;
read( u ) ;read( v ) ; read( w ) ;
ins( u ,v , w ) ;
}
spfa() ;
for( int i = ; i <= n ; i ++ ){
if( d[ i ] == inf ) {
puts( "NO PATH" ) ;
}else printf( "%d\n" , d[ i ] ) ;
}
return ;
}

[BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化的更多相关文章

  1. bzoj 2200: [Usaco2011 Jan]道路和航线——拓扑+dijkstra

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  2. BZOJ 2200: [Usaco2011 Jan]道路和航线

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  3. bzoj 2200: [Usaco2011 Jan]道路和航线【spfa】

    直接跑最短路就行了--还不用判负环 #include<iostream> #include<cstdio> #include<queue> using namesp ...

  4. 【BZOJ】2200: [Usaco2011 Jan]道路和航线

    [题意]给定n个点的图,正权无向边,正负权有向边,保证对有向边(u,v),v无法到达u,求起点出发到达所有点的最短距离. [算法]拓扑排序+dijkstra [题解]因为有负权边,直接对原图进行spf ...

  5. 2200: [Usaco2011 Jan]道路和航线 (拓扑排序+dijstra)

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  6. [Usaco2011 Jan]道路和航线

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  7. bzoj2200: [Usaco2011 Jan]道路和航线

    先忽略航线,求出图中所有连通块,再用航线拓扑排序求出每个连通块的优先级 然后dijkstra时优先处理优先级高的块里的点就行了 ps:这题SPFA会TLE #include <iostream& ...

  8. BZOJ 2200--[Usaco2011 Jan]道路和航线(最短路&拓扑排序)

    2200: [Usaco2011 Jan]道路和航线 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1128  Solved: 414[Submit] ...

  9. BZOJ 2199: [Usaco2011 Jan]奶牛议会

    2199: [Usaco2011 Jan]奶牛议会 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 375  Solved: 241[Submit][S ...

随机推荐

  1. 正则验证ip

    用python爬获取这样一条数据: <td class="ip" id="ip"><p style="display: none;& ...

  2. 【JMeter】JMeter进行简单的数据库(mysql)压力测试

    JMeter进行简单的数据库(mysql)压力测试 1.点击测试计划,再点击“浏览”,把JDBC驱动添加进来: 注:JDBC驱动一般的位置在java的安装地址下,路径类似于:    \java\jre ...

  3. 在scrapy中使用mongodb管道

    pipelines.py import json from scrapy.conf import settings from pymongo import MongoClient class SunP ...

  4. 鼠标滑动到指定位置时div固定在头部

    $(function(){ $(window).scroll(function () {            if ($(window).scrollTop() > 253) {       ...

  5. VB.net 与线程

    Imports System.Threading Imports System Public Class Form1 Dim th1, th2 As Thread Public Sub Method1 ...

  6. postman返回参数的截取

    同事在使用postman接口测试的时候,遇到这么一个问题,在一个参数里面,返回了一个类似数组的参数,如下: 然后现在需要把数组里面的两个参数分别保存到环境变量里面: 个人的想法是通过截取的方式进行数组 ...

  7. python 全局变量与局部变量

    一.引用 使用到的全局变量只是作为引用,不在函数中修改它的值的话,不需要加global关键字.如: #! /usr/bin/python a = 1 b = [2, 3] def func(): if ...

  8. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数

    Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...

  9. 课堂练习Complex类

    Complex类 #include<iostream> #include<cmath> using namespace std; class Complex { public: ...

  10. inline详解

    1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程序的局部数据(也就是 ...