您的位置:首页 > 其它

[JLOI2011]飞行路线

2016-09-12 14:04 211 查看

Description

最短路,给起点和终点,前k个点免费。


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


只要改一下d的定义,d[i][k],表示到第i个点,用k次免费。然后改操作,即可。

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