bzoj2763 [JLOI2011]飞行路线

2763: [JLOI2011]飞行路线

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3216  Solved: 1230
[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.

分析:这是一道分层图最短路的题,思路和bfs差不多,主要就是多记录一维状态表示用了多少次免费乘坐,然后就像dp一样,第i次免费乘坐能从i和i-1次转移,相当于0-1背包,最后统计答案的时候取个min就好了.这道题比较坑的一点是城市的序号是从0开始的,初始化要注意一下。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>

using namespace std;

const int inf = 0x7ffffff;

int n,m,k,head[10010],to[100010],nextt[100010],w[100010],tot,s,t,vis[10010][15],d[10010][15],ans = inf; 

struct node
{
    int x,use;
};

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

void spfa()
{
    queue <node> q;
    for (int i = 0; i <= n; i++)
    for (int j = 0; j <= k; j++)
    d[i][j] = inf;
    vis[s][0] = 1;
    node tt;
    tt.x = s;
    tt.use = 0;
    q.push(tt);
    d[s][0] = 0;
    while (!q.empty())
    {
        node u = q.front();
        q.pop();
        int x = u.x,use = u.use;
        vis[x][use] = 0;
        for (int i = head[x]; i + 1; i = nextt[i])
        {
            int v = to[i];
            if (d[v][use] > d[x][use] + w[i])
            {
                d[v][use] = d[x][use] + w[i];
                if (!vis[v][use])
                {
                    vis[v][use] = 1;
                    node temp;
                    temp.x = v;
                    temp.use = use;
                    q.push(temp);
                }
            }
            if (use < k)
            {
                if (d[v][use + 1] > d[x][use])
                {
                    d[v][use + 1] = d[x][use];
                    if (!vis[v][use + 1])
                    {
                        vis[v][use + 1] = 1;
                        node temp;
                        temp.x = v;
                        temp.use = use + 1;
                        q.push(temp); 
                    }
                }
            }
        }
    }
    for (int i = 0; i <= k; i++)
    ans = min(ans,d[t][i]);
}

int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&n,&m,&k);
    scanf("%d%d",&s,&t);
    for (int i = 1; i <= m; i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    spfa();
    printf("%d\n",ans);
    
    return 0;
} 

 

posted @ 2017-08-20 11:52  zbtrs  阅读(242)  评论(0编辑  收藏  举报