bzoj 2763 飞行路线(分层图??)
来源:互联网 发布:淘宝店铺鞋子logo 编辑:程序博客网 时间:2024/05/17 22:16
bzoj2763 飞行路线
思路:这题貌似是用分层图来做,但是我也不知道这图哪里分层了(汗)。
我是这么想的,首先,对于一条路线,我们有两种选择:1、正常通过,花费增加对应的边权。2、使用一次免费通过的机会,不增加花费。若我们贪心的来考虑的话,可能是找出一条最短路然后在最短路上对边权排序,优先对边权大的边使用免费通过(以上纯属口胡),但是这么做有问题。可能出现这种情况,存在一条s到t的路径,它的边的数量较少,而边权都很大,这样的话我们再用上述的方法就不对了,所以我们要考虑一种新的方法。
我们令dist[u][g],表示从起点到u使用了g次机会的最短距离(最小花费)。那么我们可以用一种类似于动态规划的方法来想一下,对于u的一个可到达点v,我们可以选择方案1转移到dist[v][g],也可以选择方案2转移到dist[v][g+1],跑spfa,这样我们就有了所有状态,然后问题得解。(可能这么说有些抽象,具体实现请看代码)。
PS:说实话我真觉得这就是动态规划,其实SPFA也是基于动态规划的一种算法。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int N=10000+5;const int M=50000+5;int read(){ int ret=0,f=1; char c=getchar(); while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();} while(c>='0'&&c<='9') {ret=ret*10+c-'0';c=getchar();} return ret*f;}int n,m,K;struct edge{ int from,to,dis;}e[M<<1];int head[N],nxt[M<<1],tot=0;void adde(int f,int t,int d){ e[++tot]=(edge){f,t,d}; nxt[tot]=head[f]; head[f]=tot;}int dist[N][12];bool inq[N][12];struct zt{ int id,g;};queue<zt> q;void spfa(int st){ dist[st][0]=0; q.push((zt){st,0}); inq[st][0]=1; while(!q.empty()) { int u=q.front().id; int gg=q.front().g; q.pop(); inq[u][gg]=0; for(int i=head[u];~i;i=nxt[i]) { int v=e[i].to; if(dist[v][gg]>dist[u][gg]+e[i].dis) { dist[v][gg]=dist[u][gg]+e[i].dis; if(!inq[v][gg]) { q.push((zt){v,gg}); inq[v][gg]=1; } } if(gg+1<=K&&dist[v][gg+1]>dist[u][gg]) { dist[v][gg+1]=dist[u][gg]; if(!inq[v][gg+1]) { q.push((zt){v,gg+1}); inq[v][gg+1]=1; } } } }}int main(){ memset(dist,0x3f,sizeof(dist)); memset(head,-1,sizeof(head)); n=read(),m=read(),K=read(); int st,ed; st=read(),ed=read(); int a,b,c; for(int i=1;i<=m;i++) { a=read(),b=read(),c=read(); adde(a,b,c); adde(b,a,c); } spfa(st); int ans=2147483647; for(int i=0;i<=K;i++) ans=min(ans,dist[ed][i]); printf("%d\n",ans); return 0;}
阅读全文
0 0
- bzoj 2763 飞行路线(分层图??)
- BZOJ 2763: [JLOI2011]飞行路线(分层图最短路)
- BZOJ 2763 飞行路线(spfa+分层图)
- 【BZOJ 2763 JLOI 2011】飞行路线 (SPFA+分层图)
- (bzoj 2763 [JLOI2011]飞行路线)<分层图>
- 【bzoj 2763】T13 飞行路线(Spfa+分层图)
- bzoj 2763 飞行路线(分层图最短路)
- BZOJ 2763 JLOI2011 飞行路线 分层图+堆优化SPFA
- BZOJ 2763 JLOI 2011 飞行路线 分层图+最短路
- bzoj 2763 JLOI 2011 飞行路线 分层图+spfa
- bzoj 2763: [JLOI2011]飞行路线 分层图最短路
- BZOJ 2763: [JLOI2011]飞行路线 分层图最短路
- bzoj 2763 [JLOI2011]飞行路线 Dijikstra 分层
- BZOJ[2763]飞行路线
- BZOJ 2763 飞行路线
- BZOJ 2763 飞行路线(最短路)
- poj 2763: [JLOI2011]飞行路线(spfa分层图最短路)
- [bzoj2763]2763: [JLOI2011]飞行路线 分层图最短路
- MySQL代码整理
- centos6.5 yum install nginx
- 数据库查询语言分类
- 彻底弄懂Activity四大启动模式
- iOS开发iOS8扩展App Extension 中文版
- bzoj 2763 飞行路线(分层图??)
- 重新学习Linux
- springBoot集成shiro+redis遇到的坑
- LoadRunner测试多个用户账号是否可用
- 精通python网络爬虫笔记一
- 客户端app的demo的实现
- 【腾讯TMQ】像google一样测试系列之一:目录篇
- DOM对象和Jquery对象
- 剑指offer第20题(包含min函数的栈)