[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 ...
随机推荐
- xls的读写
import xlrd # 读取xls文件 # 打开xls文件 data = xlrd.open_workbook('x1.xls') print(type(data)) # 获取表Sheet1 ta ...
- 查看修改MySQL字符集
查看修改MySQL字符集 http://blog.sina.com.cn/s/blog_70ac6bec01016fts.html 查看修改MySQL字符集 (2012-08-22 09:53:21) ...
- 001-nginx基础配置-location
一.基础语法 Location block 的基本语法形式是: location [=|~|~*|^~|@] pattern { ... } [=|~|~*|^~|@] 被称作 location mo ...
- [py]你真的了解多核处理器吗? 了解多线程
越来越多的人搞爬虫,设计到多线程爬取, 还有一些机器学习的一些模块也需要这玩意, 感觉自己不会逼格不高. 抽时间赶紧玩一玩这东西, 希望提高对软件的认知和归属感,不要太傻. cpu内部架构参考 你知道 ...
- Chrome插件汇总
Chrome浏览器可以增加很多插件,帮助我们更方便地使用. 1 重新定位新标签页 名字:Infinity.crx 官网:http://www.infinitynewtab.com/ 效果图如下: ...
- 测试人员需要了解的sql知识(基础篇)
这是第一篇关于数据库的,本着详细的原则,基础的还是不能放过,还是那句话,有问题,欢迎指出! ------------------------------------------------------ ...
- 如何快速获取ListView的打气筒对象
简单的方式有三种: @Override public View getView(int position, View convertView, ViewGroup parent) { View vie ...
- How to enable TLS 1.2 on Windows Server 2008 R2
Problem How to enable TLS 1.2 on Windows Server 2008 R2? Resolution QuoVadis recommends enabling and ...
- print(array)时array中间是省略号没有输出全部的解决方法
import numpy as np np.set_printoptions(threshold=np.inf) 大量元素情况 可以采用set_printoptions(threshold='nan' ...
- mysql中char和varchar详解
一.首先创建表. CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL, `a` char(255) DEFAULT NULL) ENGINE=InnoDB DE ...