【NOIP2014 Day2 T2】寻找道路
2015-09-04 21:48
465 查看
【NOIP2014 Day2 T2】寻找道路
Time Limit:10000MS Memory Limit:131072K
Total Submit:33 Accepted:25
Case Time Limit:1000MS
Description
![](http://oi.nks.edu.cn/images/3107-1.jpg)
Input
![](http://oi.nks.edu.cn/images/3107-2.jpg)
Output
![](http://oi.nks.edu.cn/images/3107-3.jpg)
Sample Input
Sample Output
Hint
![](http://oi.nks.edu.cn/images/3107-4.jpg)
![](http://oi.nks.edu.cn/images/3107-5.jpg)
![](http://oi.nks.edu.cn/images/3107-6.jpg)
Source
感谢nodgd放上题目
最短路的反向边思路
用了栈优化bellman-ford,T了,用SPFA就A了,然后Dijkstra+heap是过不了(没试过,((V+E)logV)理论是过不了的),用了bellman-ford+stack(传说中比SPFA还不稳定),只是有时有奇效。
Time Limit:10000MS Memory Limit:131072K
Total Submit:33 Accepted:25
Case Time Limit:1000MS
Description
![](http://oi.nks.edu.cn/images/3107-1.jpg)
Input
![](http://oi.nks.edu.cn/images/3107-2.jpg)
Output
![](http://oi.nks.edu.cn/images/3107-3.jpg)
Sample Input
样例输入1: 3 2 1 2 2 1 1 3 样例输入2: 6 6 1 2 1 3 2 6 2 5 4 5 3 4 1 5
Sample Output
样例输出1: -1 样例输出2: 3
Hint
![](http://oi.nks.edu.cn/images/3107-4.jpg)
![](http://oi.nks.edu.cn/images/3107-5.jpg)
![](http://oi.nks.edu.cn/images/3107-6.jpg)
Source
感谢nodgd放上题目
最短路的反向边思路
用了栈优化bellman-ford,T了,用SPFA就A了,然后Dijkstra+heap是过不了(没试过,((V+E)logV)理论是过不了的),用了bellman-ford+stack(传说中比SPFA还不稳定),只是有时有奇效。
#include<cstdio> #include<queue> using namespace std; #define maxn 10009 #define maxm 200009 #define inf 2100000000 queue <int>stk;//一开始用的stack,T了,改成queue时就懒得改变量名了。 int n, m; int vt=0; int end[maxm], len[maxm], next[maxm], last[maxn], start[maxm]; int dis[maxn]; bool is_in[maxn], can_arr[maxn]; void adde(int s, int e, int l) { vt++; start[vt]=s; end[vt]=e; len[vt]=l; next[vt]=last[s]; last[s]=vt; } void bellman_ford(int s) { int i, cur; for(i=1; i<=n; i++) dis[i]=inf; dis[s]=0; stk.push(s); is_in[s]=true; while(!stk.empty()) { cur=stk.front(); stk.pop(); is_in[cur]=false; for(i=last[cur]; i; i=next[i]) { if(dis[end[i]]>dis[cur]+len[i]) { dis[end[i]]=dis[cur]+len[i]; if(!is_in[end[i]]) { stk.push(end[i]); is_in[end[i]]=true; } } } } } inline void _read(int& d) { char t=getchar();bool f=false; while(t<'0'||t>'9') {if(t=='-') f=true; t=getchar();} for(d=0;t<='9'&&t>='0';t=getchar()) d=d*10+t-'0'; if(f) d=-d; } inline void _out(int d) { int o[30],top=1; if(d==0){putchar('0');return ;} if(d<0) {putchar('-');d=-d;} while(d) { o[top++]=d%10; d/=10; } for(--top;top;--top) putchar('0'+o[top]); } int main() { int i, j; int ts, te, s, e; scanf("%d%d", &n, &m); for(i=1; i<=m; i++) { _read(ts), _read(te); if(ts!=te) { adde(te, ts, 1); } } scanf("%d%d", &s, &e); for(i=1; i<=n; i++) can_arr[i]=true; bellman_ford(e); for(i=1; i<=vt; i++) { if(dis[start[i]]==inf) { len[i]=inf; can_arr[end[i]]=false; } } for(i=1; i<=vt; i++) { if(can_arr[end[i]]==false) { len[i]=inf; } } bellman_ford(e); printf("%d", dis[s]==inf?(-1):dis[s]); return 0; }
相关文章推荐
- Pop–实现任意iOS对象的任意属性的动态变化
- LeetCode ||Ugly Number II
- 原子变量(AtomicLong, AtomicInteger, AtomicReference)
- COCI2014/2015CONTEST #3 dom
- hdu 1005 Number Sequence(找规律)
- Apache之——多虚拟主机多站点配置的两种实现方案
- S5PV210开发系列九_网卡驱动实现
- COCI2014 dom
- 递归函数时间复杂度分析
- BigDecimal.ZERO
- nodejs小问题:[1]express不是内部或外部命令
- CSS行高——line-height
- COCICONTEST# 29.11.2014# 题解+总结
- JDBC连接sql server数据库操作
- 为MySQL选择合适的备份方式
- 策略模式
- 华为HG8245C光猫破解用户连接数限制
- sqlite数据库中常用的sql语句
- 怎么求人眼图像中的噪声
- 华为机试测试-验证尼科彻斯定理