BZOJ_P2763 [JLOI2011]飞行路线(分层图+最短路)
2016-03-27 07:55
381 查看
BZOJ传送门
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1725 Solved: 647
[Submit][Status][Discuss]
Description
Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?
Input
数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t< n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b< n,a与b不相等,0<=c<=1000)
Output
只有一行,包含一个整数,为最少花费。
Sample Input
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
8
HINT
对于30%的数据,2<=n<=50,1<=m<=300,k=0;
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据 ,2<=n<=10000,1<=m<=50000,0<=k<=10.
Source
Sol:
k辣么小就分层
将j层连(j+1)层,费用为0就可以了
直接跑最短路!据说SPFA会TLE?
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1725 Solved: 647
[Submit][Status][Discuss]
Description
Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?
Input
数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t< n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b< n,a与b不相等,0<=c<=1000)
Output
只有一行,包含一个整数,为最少花费。
Sample Input
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
8
HINT
对于30%的数据,2<=n<=50,1<=m<=300,k=0;
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据 ,2<=n<=10000,1<=m<=50000,0<=k<=10.
Source
Sol:
k辣么小就分层
将j层连(j+1)层,费用为0就可以了
直接跑最短路!据说SPFA会TLE?
#include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; #define N 110005 inline int in(int x=0,char ch=getchar()){while(ch>'9'||ch<'0') ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x;} int n,m,k,s,t,ans;int d ;bool b ; struct Edge{int to,w;}; vector<Edge> g ; inline void Add_Edge(int fr,int to,int w){g[fr].push_back((Edge){to,w});} struct Heap{int to;long long d;bool operator < (const Heap &a)const{return d>a.d;}}; void Dijkstra(){ priority_queue<Heap> q;q.push((Heap){s,0}); memset(d,0x7f,sizeof(d));d[s]=0;int x; while(!q.empty()){ x=q.top().to;q.pop(); if(b[x]) continue;b[x]=1; for(int i=0,v,lim=g[x].size();i<lim;i++){ if(d[x]+g[x][i].w<d[v=g[x][i].to]){ d[v]=d[x]+g[x][i].w; q.push((Heap){v,d[v]}); } } } } int main(){ n=in(),m=in(),k=in();s=in(),t=in();int u,v,w; for(int i=1;i<=m;i++){ u=in(),v=in(),w=in(); for(int j=0;j<=k;j++){ Add_Edge(j*n+u,j*n+v,w);Add_Edge(j*n+v,j*n+u,w); if(j<k) Add_Edge(j*n+u,(j+1)*n+v,0),Add_Edge(j*n+v,(j+1)*n+u,0); } } Dijkstra();ans=0x7fffffff; for(int i=0;i<=k;i++) ans=min(ans,d[i*n+t]); printf("%d\n",ans); return 0; }
相关文章推荐
- 关系型数据库工作原理-高速缓存(翻译自Coding-Geek文章)
- android activity 开启 数据传递
- 20145308刘昊阳 《Java程序设计》第4周学习总结
- Indy10 FTP列目录乱码问题
- Win10移动版红石预览版14295已知问题、解决方案以及更新(修复)内容汇总
- 山西疫苗案源于制度漏洞 如严查不会出现山东5.7亿疫苗案发生
- 关于验证码的首次加载失效问题
- 软硬件调试九法:第四条规则 分而治之
- BestCoder Round #77 (div.2)解题报告
- scrollView中contentSize、contentInset和contentOffset区别
- Win10桌面版红石预览版14295更新(修复)、已知问题及解决方案汇总
- P2P实时音视频之NAT穿越
- 获取Web.config配置节
- 针对Linux ASP.NET MVC网站中 httpHandlers配置无效的解决方案
- 值得推荐的微软技术公众号推荐
- [1] 无线通信:基本概念
- Runtime(六)
- Runtime(五)
- runtim(四)
- codevs 1115 开心的金明--01背包