洛谷P1266 速度限制
2017-08-11 17:32
239 查看
题目链接:https://www.luogu.org/problem/show?pid=1266
解题思路:
1.求最快路线类似于求最短路,想到用spfa解决。
2.和一般的spfa不同,本题的路径中是以距离/速度来更新答案的,并且速度存在两种情况,需要分别分析。
3.既然有了速度这个变量,我们需要用数组来储存记录速度,dis[i][j]表示从到i点时速度为j的最快路线。在更新最短路的时候判断一下速度是否为0,用不同的速度更新答案。
4.题目要求输出路径,需要记录点和速度,递归输出路径。
解题思路:
1.求最快路线类似于求最短路,想到用spfa解决。
2.和一般的spfa不同,本题的路径中是以距离/速度来更新答案的,并且速度存在两种情况,需要分别分析。
3.既然有了速度这个变量,我们需要用数组来储存记录速度,dis[i][j]表示从到i点时速度为j的最快路线。在更新最短路的时候判断一下速度是否为0,用不同的速度更新答案。
4.题目要求输出路径,需要记录点和速度,递归输出路径。
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #define pr pair<int,int> using namespace std; queue<pair<int,int> >q; struct node{ int from,to,t,ds; }list[505*505]; double dis[505][505]; bool v[255][505]; int n,m,k,s,ans,x,y,t,z,head[255],px[255][505],ps[255][505]; void add(int x,int y,int t,int z){ list[++s].from=head[x]; list[s].to=y; list[s].t=t; list[s].ds=z; head[x]=s; } void spfa(){ memset(dis,66,sizeof dis); v[1][70]=1;q.push(pr(1,70));dis[1][70]=0; while(!q.empty()){ pr p=q.front();q.pop(); int x=p.first,y=p.second;v[x][y]=0; for (int i=head[x];i;i=list[i].from){ if (list[i].t==0) { if (dis[list[i].to][y]>dis[x][y]+1.0*list[i].ds/y){ dis[list[i].to][y]=dis[x][y]+1.0*list[i].ds/y; px[list[i].to][y]=x; ps[list[i].to][y]=y; if (!v[list[i].to][y]){ v[list[i].to][y]=1; q.push(pr(list[i].to,y)); } } } else { int sp=list[i].t; if (dis[list[i].to][sp]>dis[x][y]+1.0*list[i].ds/sp){ dis[list[i].to][sp]=dis[x][y]+1.0*list[i].ds/sp; px[list[i].to][sp]=x; ps[list[i].to][sp]=y; if (!v[list[i].to][sp]){ v[list[i].to][sp]=1; q.push(pr(list[i].to,sp)); } } } } } } void print(int a,int b) { if(a!=1)print(px[a][b],ps[a][b]); printf("%d ",a-1); } int main(){ cin>>n>>m>>k;k++; for (int i=1;i<=m;i++){ scanf("%d%d%d%d",&x,&y,&t,&z); x++;y++; add(x,y,t,z); } spfa(); double ans=1e30;int c; for (int i=1;i<=500;i++){ if (ans>dis[k][i]) ans=dis[k][i],c=i; } print(px[k][c],ps[k][c]); cout<<k-1; return 0; }
相关文章推荐
- spfa裂点——洛谷P1266 速度限制
- 洛谷P1266——速度限制
- 使用charles抓包工具模拟网络状况,限制网络速度
- Flume-ng 数据发送速度限制
- 思科路由器实现ip速度限制
- 收藏:ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小) (转自: Arhrun)
- nginx 限制ip并发数和限制速度
- 十六.更强大的防攻击(访问速度限制)模块,对Nginx的limit_req增强
- 为程序增加处理速度的限制
- 通过Chrome模拟和调试网速慢的情况来限制一些P2P视频网站上传速度占满的情况
- Nginx并发数、每秒连接数、下载速度限制,防攻击杀手锏
- 简单的js限制鼠标点击速度方法
- win8系统如何限制网速传输速度?win8系统限制网速传输速度的方法
- nginx 限制指定ip的并发数和 速度
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- Java服务端限制下载速度
- 【SPFA+拆点】速度限制
- Centos 中限制网络带宽速度
- 用ROS限制土豆优酷等在线视频网站速度【图+脚本】
- 分析与处理局域网速度限制工作