【bzoj2763】[JLOI2011]飞行路线

2014年3月25日1,7260

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.

【分析】这是一个分层图最短路的题。题中说对于最短路,其中有k条边可以免费,那么我们就建k层图。对于当前节点,我可以在本层跑,我也可以往上一层,即该条边免费,前提是已经免费的边的条数<k。则用dij即可。

#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int N=6e4+;
const int mod=1e9+;
int n,m,s,k,t,cnt,idl[N<<],idr[N<<];
bool vis[N][];
LL d[N][];
vector<pii>edg[N];
struct man{
int v;
int c;
LL w;
bool operator<(const man &e)const{
return w>e.w;
}
};
priority_queue<man>q;
void dij(int s){
memset(d,-,sizeof d);memset(vis,,sizeof vis);
d[s][]=;
q.push(man{s,,});
while(!q.empty()){
int u=q.top().v,c=q.top().c;q.pop(); if(vis[u][c])continue;
vis[u][c]=;
for(int i=;i<edg[u].size();++i){
int v=edg[u][i].first,w=edg[u][i].second;
if(!vis[v][c]&&(d[v][c]==-||d[v][c]>d[u][c]+w)){
d[v][c]=d[u][c]+w;
q.push(man{v,c,d[v][c]});
}
if(c<k){
if(!vis[v][c+]&&(d[v][c+]==-||d[v][c+]>d[u][c])){
d[v][c+]=d[u][c];
q.push(man{v,c+,d[v][c+]});
}
}
}
}
}
int main()
{
int x,y,w;
scanf("%d%d%d",&n,&m,&k);
scanf("%d%d",&s,&t);
while(m--)
{
scanf("%d%d%d",&x,&y,&w);
edg[x].push_back(make_pair(y,w));
edg[y].push_back(make_pair(x,w));
}
dij(s);
LL ans=;
for(int i=;i<=k;i++)ans=min(ans,d[t][i]);
printf("%lld\n",ans);
return ;
}

最新文章

  1. TDD在Unity3D游戏项目开发中的实践
  2. 【leetcode❤python】 204. Count Primes
  3. zTree开发下拉树
  4. Scalaz(39)- Free :a real monadic program
  5. 【Android Studio错误】 If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle.
  6. 用javaScript实现 登陆记住密码功能。
  7. 想调试,装了个Zend Server
  8. python 使用__future__
  9. 在Eclipse中安装ADT
  10. HDU_2018——母牛产小牛的问题,递推
  11. WorkFlow WF如何为一个集合赋值
  12. C# Excel或表格插件
  13. [原] Jenkins Android 自动打包配置(转)
  14. AJAX 表单提交 文件上传
  15. getMemory的经典例子
  16. 关于js中close()方法的兼容性问题
  17. [Python设计模式] 第21章 计划生育——单例模式
  18. PS文字生成头像
  19. monit安装配置
  20. 用canvas画三角形的方法

热门文章

  1. 【题解】AHOI2009中国象棋
  2. [洛谷P1420]最长连号
  3. 用JavaScript实现一个简单的树结构
  4. Sencha touch中Ext.List的使用及高度自适应
  5. 在eclipse中使用JUnit4,以及使用JUnit4进行单元测试的技巧
  6. oracle 包和包实现
  7. 转:使用 Nginx Upload Module 实现上传文件功能
  8. Spring - IoC(7): 延迟实例化
  9. Web.xml过滤器配置及执行顺序概念
  10. Splunk笔记