您的位置:首页 > 编程语言 > C语言/C++

[BZOJ2763][JLOI2011]飞行路线

2016-09-18 13:40 267 查看
提交地址

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.

思路:更改一下f数组的定义,把它变为2维。

f[i][j]表示到达i号点用了j次免费机会的最短路。

至于怎么推下一次状态也很简单,就分2种情况,用免费机会和不要免费机会。

代码如下

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node
{
int x,y,c,next;
}a[210000];int len,last[110000];
void ins(int x,int y,int c)
{
len++;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len;
}
bool v[110000][11];
int list[110000][11];
int d[110000][11],head,tail;
int n,m,kk,st,ed;
int main()
{
len=0;memset(last,0,sizeof(last));
scanf("%d%d%d%d%d",&n,&m,&kk,&st,&ed);
for(int i=1;i<=m;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);
ins(y,x,c);
}
memset(d,31,sizeof(d));d[st][0]=0;
memset(v,false,sizeof(v));
v[st][0]=true;list[1][0]=st;list[1][1]=0;
head=1;tail=2;
while(head!=tail)
{
int x=list[head][0],c=list[head][1];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(d[x][c]+a[k].c<d[y][c])
{
d[y][c]=d[x][c]+a[k].c;
if(v[y][c]==false)
{
v[y][c]=true;
list[tail][0]=y;
list[tail++][1]=c;
if(tail==100001)tail=1;
}
}
if(d[x][c]<d[y][c+1] && c<kk)
{
d[y][c+1]=d[x][c];
if(v[y][c+1]==false)
{
v[y][c+1]=true;
list[tail][0]=y;
list[tail++][1]=c+1;
if(tail==100001)tail=1;
}
}
}
head++;
if(head==100001)head=1;
v[x][c]=false;
}
int ans=999999999;
for(int i=0;i<=kk;i++)ans=min(ans,d[ed][i]);
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ BZOJ