• [BZOJ2763][JLOI2011]飞行路线


    2763: [JLOI2011]飞行路线

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

     
    拆点,将每个点拆为k个点,为k层图,同一层直接连边,不同层连花费为0的边,跑最短路。
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<queue>
     8 using namespace std;
     9 int n,m,k,s,t;
    10 struct edge
    11 {
    12     int to,next,v;
    13 }e[3000001];
    14 struct data
    15 {
    16     int p,d;
    17     bool operator<(const data t)const
    18     {
    19         return d>t.d;
    20     }
    21 };
    22 int head[200002],cnt;
    23 void add(int u,int v,int val){e[cnt].next=head[u];e[cnt].v=val;e[cnt].to=v;head[u]=cnt;cnt++;}
    24 int dis[200002];
    25 int vis[200002];
    26 int dijkstra()
    27 {
    28     memset(dis,97,sizeof(dis));
    29     memset(vis,0,sizeof(vis));
    30     priority_queue<data> q;
    31     dis[s]=0;
    32     q.push((data){s,0});
    33     while(!q.empty())
    34     {
    35         data now=q.top();q.pop();
    36         if(vis[now.p]) continue;
    37         vis[now.p]=1;
    38         for(int i=head[now.p];i>=0;i=e[i].next)
    39         {
    40             if(dis[e[i].to]>dis[now.p]+e[i].v)
    41             {
    42                 dis[e[i].to]=dis[now.p]+e[i].v;
    43                 q.push((data){e[i].to,dis[e[i].to]});
    44             }
    45         }
    46     }
    47     int ans=2147483647;
    48     for (int i=0;i<=k;i++) ans=min(ans,dis[i*n+t]);
    49     printf("%d",ans);
    50 }
    51 int main()
    52 {
    53     memset(head,-1,sizeof(head));
    54     scanf("%d%d%d%d%d",&n,&m,&k,&s,&t);
    55     for(int i=1;i<=m;i++)
    56     {
    57         int u,v,val;
    58         scanf("%d%d%d",&u,&v,&val);
    59         for(int j=0;j<=k;j++)
    60         {
    61             add(j*n+u,j*n+v,val);
    62             add(j*n+v,j*n+u,val);
    63             if(j<k)
    64             {
    65                 add(j*n+u,(j+1)*n+v,0);
    66                 add(j*n+v,(j+1)*n+u,0);
    67             }
    68         }
    69     }
    70     dijkstra();
    71 }
    View Code
     
    O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~
  • 相关阅读:
    使用AOP 实现Redis缓存注解,支持SPEL
    springmvc 双亲上下文导致的 No mapping found for HTTP request
    调用iframe 中的js[兼容各种浏览器]
    NIO学习:异步IO实例
    byte数组与对象之间的相互转换
    NIO学习:buffer读入与写出(文件复制示例)
    NIO学习:使用Channel、Buffer写入文件
    使用摘要流获取文件的MD5
    ActiveMQ入门实例
    获取java项目 classpath目录
  • 原文地址:https://www.cnblogs.com/wls001/p/7389290.html
Copyright © 2020-2023  润新知