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

[JLOI2011]飞行路线

2016-09-09 13:55 246 查看
bzoj2763

↑↑↑

题目

思路:

更改一下d数组的定义,把它变为2维。

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

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

就是这样

↓↓↓

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])
{
v[y][c+1]=true;
list[tail][0]=y;list[tail++][1]=c+1;
if(tail==100001) tail=1;
}
}


#include<cstdio>
#include<cstring>
#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;
}
int d[110000][11];
int list[110000][11],head,tail;
bool v[110000][11];
int main()
{
int n,m,kk,st,ed;
scanf("%d%d%d",&n,&m,&kk);
scanf("%d%d",&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];int 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]) { 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;
}


9867
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言