[BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化
Description
Input
Output
Sample Input
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
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优化的更多相关文章
- bzoj 2200: [Usaco2011 Jan]道路和航线——拓扑+dijkstra
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- BZOJ 2200: [Usaco2011 Jan]道路和航线
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- bzoj 2200: [Usaco2011 Jan]道路和航线【spfa】
直接跑最短路就行了--还不用判负环 #include<iostream> #include<cstdio> #include<queue> using namesp ...
- 【BZOJ】2200: [Usaco2011 Jan]道路和航线
[题意]给定n个点的图,正权无向边,正负权有向边,保证对有向边(u,v),v无法到达u,求起点出发到达所有点的最短距离. [算法]拓扑排序+dijkstra [题解]因为有负权边,直接对原图进行spf ...
- 2200: [Usaco2011 Jan]道路和航线 (拓扑排序+dijstra)
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- [Usaco2011 Jan]道路和航线
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- bzoj2200: [Usaco2011 Jan]道路和航线
先忽略航线,求出图中所有连通块,再用航线拓扑排序求出每个连通块的优先级 然后dijkstra时优先处理优先级高的块里的点就行了 ps:这题SPFA会TLE #include <iostream& ...
- BZOJ 2200--[Usaco2011 Jan]道路和航线(最短路&拓扑排序)
2200: [Usaco2011 Jan]道路和航线 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1128 Solved: 414[Submit] ...
- BZOJ 2199: [Usaco2011 Jan]奶牛议会
2199: [Usaco2011 Jan]奶牛议会 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 375 Solved: 241[Submit][S ...
随机推荐
- 正则验证ip
用python爬获取这样一条数据: <td class="ip" id="ip"><p style="display: none;& ...
- 【JMeter】JMeter进行简单的数据库(mysql)压力测试
JMeter进行简单的数据库(mysql)压力测试 1.点击测试计划,再点击“浏览”,把JDBC驱动添加进来: 注:JDBC驱动一般的位置在java的安装地址下,路径类似于: \java\jre ...
- 在scrapy中使用mongodb管道
pipelines.py import json from scrapy.conf import settings from pymongo import MongoClient class SunP ...
- 鼠标滑动到指定位置时div固定在头部
$(function(){ $(window).scroll(function () { if ($(window).scrollTop() > 253) { ...
- VB.net 与线程
Imports System.Threading Imports System Public Class Form1 Dim th1, th2 As Thread Public Sub Method1 ...
- postman返回参数的截取
同事在使用postman接口测试的时候,遇到这么一个问题,在一个参数里面,返回了一个类似数组的参数,如下: 然后现在需要把数组里面的两个参数分别保存到环境变量里面: 个人的想法是通过截取的方式进行数组 ...
- python 全局变量与局部变量
一.引用 使用到的全局变量只是作为引用,不在函数中修改它的值的话,不需要加global关键字.如: #! /usr/bin/python a = 1 b = [2, 3] def func(): if ...
- 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__)返回 ...
- 课堂练习Complex类
Complex类 #include<iostream> #include<cmath> using namespace std; class Complex { public: ...
- inline详解
1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程序的局部数据(也就是 ...