zoukankan      html  css  js  c++  java
  • 1269 匈牙利游戏

    1269 匈牙利游戏

     

    2012年CCC加拿大高中生信息学奥赛

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 钻石 Diamond
     
     
     
    题目描述 Description

    Welcome to the Hungary Games! The streets of Budapest form a twisted network of one-way streets.

    欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络。

    You have been forced to join a race as part of a “Reality TV” show where you race through these streets, starting at the Sz´echenyi thermal bath (s for short) and ending at the Tomb of G¨ ul Baba (t for short).

    你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束。

    Naturally, you want to complete the race as quickly as possible, because you will get more promo- tional contracts the better you perform.

    很自然的,你想要尽快的完成比赛,因为你的比赛完成的越好,你就能得到更多的商业促销合同。

    However, there is a catch: any person who is smart enough to take a shortest s-t route will be thrown into the P´alv¨olgyi cave system and kept as a national treasure. You would like to avoid this fate, but still be as fast as possible. Write a program that computes a strictly-second-shortest s-t route.

    但是,有一个需要了解的是,如果有人过于聪明找到从s到t的最短路线,那么他就被扔到国家极品人类保护系统中作为一个国家宝藏收藏起来。你显然要避免这种事情的发生,但是也想越快越好。写一个程序来计算一个从s到t的严格次短路线吧。

    Sometimes the strictly-second-shortest route visits some nodes more than once; see Sample Input 2 for an example.

    有的时候,严格次短路线可能访问某些节点不止一次。样例2是一个例子。

    输入描述 Input Description

    The first line will have the format N M, where N is the number of nodes in Budapest and M is the number of edges. The nodes are 1,2,...,N; node 1 represents s; node N represents t. Then there are M lines of the form A B L, indicating a one-way street from A to B of length L. You can assume that A != B on these lines, and that the ordered pairs (A,B) are distinct.

    第一行包含两个整数N和M,N代表布达佩斯的节点个数,M代表边的个数。节点编号从1到N。1代表出发点s,N代表终点t。接下来的M行每行三个整数A B L,代表有一条从A到B的长度为L的单向同路。你可以认为A不等于B,也不会有重复的(A,B)对。

    输出描述 Output Description

    Output the length of a strictly-second-shortest route from s to t. If there are less than two possible lengths for routes from s to t, output −1.

    输出从s到t的严格次短路的长度。如果从s到t的路少于2条,输出-1。

    样例输入 Sample Input

    样例输入1:

    4 6

    1 2 5

    1 3 5

    2 3 1

    2 4 5

    3 4 5

    1 4 13

    样例输入2:

    2 2

    1 2 1

    2 1 1

    样例输出 Sample Output

    样例输出1:

    11

    样例输出2:

    3

    数据范围及提示 Data Size & Hint

    对于样例1:

    There are two shortest routes of length 10 (1 → 2 → 4,1 → 3 → 4) and the strictly-second- shortest route is 1 → 2 → 3 → 4 with length 11.

    对于样例2:

    The shortest route is 1 → 2 of length 1, and the strictly-second route is 1 → 2 → 1 → 2 of length 3.

    一种思路:

    严格次短路问题(不能重复最短路做次短路) 

    三种情况
    1.最短路可以被最短路更新,次短路可以被更新前的最短路更新
    2.最短路无法更新,次短路可以被更新
    3.次短路可以被次短路更新

    调试的时候可能会出现以下情况,我也不知道为什么,但确实可以AC。

      AC代码1:

    #include<cstdio>
    #include<cstring>
    #include<deque>
    using namespace std;
    const int N=1e5+10;
    int tot,next[N<<1],head[N];
    int pre[N],dis[N];
    bool vis[N];
    struct node{
        int v,w;
    }e[N];
    inline void add(int x,int y,int z){
        e[++tot].v=y;e[tot].w=z;next[tot]=head[x];head[x]=tot;
    }
    deque<int>q;//双端队列 
    void spfa(int s){
        memset(dis,0x3f,sizeof dis);//初始化一个极大值,不然不给过QAQ别问我怎么知道的
        memset(pre,0x3f,sizeof pre);
        memset(vis,0,sizeof vis);
        q.push_back(0); 
        q.push_back(s);
        dis[s]=0;
        vis[s]=1;
        while(!q.empty()){
            int u=q.front();q.pop_front();
            vis[u]=0;
            for(int i=head[u];i;i=next[i]){
                int v=e[i].v;
                if(dis[v]>dis[u]+e[i].w){
                    pre[v]=dis[v];//更新后的最短路是最短路,更新之前的最短路是当前的次短路
                    dis[v]=dis[u]+e[i].w;
                    if(dis[v]<dis[q.front()]) 
                        q.push_front(v);
                    else 
                        q.push_back(v);
                }//如果可以更新最短路,那么就让它更新吧
                else if(pre[v]>dis[u]+e[i].w&&dis[v]<dis[u]+e[i].w){
                    pre[v]=dis[u]+e[i].w;
                    if(dis[v]<dis[q.front()]) 
                        q.push_front(v);
                    else 
                        q.push_back(v);
                }//如果不能更新最短路,但是可以更新次短路,那么就让它更新吧
                else if(pre[v]>pre[u]+e[i].w){
                    pre[v]=pre[u]+e[i].w;
                    if(dis[v]<dis[q.front()]) 
                        q.push_front(v);
                    else 
                        q.push_back(v);
                }//次短路可以更新次短路,那么就让它更新吧
            }
        } 
    }
    int main(){
        int n,m,x,y,z;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z);
        spfa(1);    
        printf("%d
    ",pre[n]>0&&pre[n]<1061109567?pre[n]:-1);    
        return 0;
    }

     来自题解思路的AC代码:(调试无异常)

     AC代码2:

    /*
       从前向后spfa一遍,再把边反向spfa一遍,求出dis1和dis2
       枚举每条边,设这条边一定要走,求出这种情况下的最短路,最后在所有情况中找出严格次短路
       对于这个方法的理解:
       因为如果求不出严格次短路,说明严格次短路中的某条路没有走过,那么我们可以让这条路一定走,
       那么经过这条路的最短路一定是严格次短路,所以要把所有的点枚举一遍
       注意:这个方法求不出次短路,因为如果这样做,最短路可能走过两遍,但我们是分辨不出来的,
       这对本题没有影响,因为严格次短路的长度与最短路不同,但次短路可能与最短路长度相同,
       所以不对。 
    */
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define N 100010
    #define M 20010
    struct node{
        int v,w,next;
    }e1[N<<1],e2[N<<1];
    int n,m,cnt,t1,t2,f[N<<2],head1[M],head2[M],dis1[M],dis2[M];
    bool vis[M];
    inline void add1(int x,int y,int z){
        e1[++t1].v=y;e1[t1].w=z;e1[t1].next=head1[x];head1[x]=t1;
    }
    inline void add2(int x,int y,int z){
        e2[++t2].v=y;e2[t2].w=z;e2[t2].next=head2[x];head2[x]=t2;
    }
    inline void spfa1(){
        memset(dis1,63,sizeof dis1);
        memset(vis,0,sizeof vis);
        vis[1]=1;
        dis1[1]=0;
        queue<int>q;
        q.push(1);
        while(!q.empty()){
            int p=q.front();q.pop();
            vis[p]=0;
            for(int i=head1[p];i;i=e1[i].next){
                int v=e1[i].v,w=e1[i].w;
                if(dis1[v]>dis1[p]+w){
                    dis1[v]=dis1[p]+w;
                    if(!vis[v]){
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        } 
    }
    inline void spfa2(){
        memset(dis2,63,sizeof dis2);
        memset(vis,0,sizeof vis);
        vis[n]=1;
        dis2[n]=0;
        queue<int>q;
        q.push(n);
        while(!q.empty()){
            int p=q.front();q.pop();
            vis[p]=0;
            for(int i=head2[p];i;i=e2[i].next){
                int v=e2[i].v,w=e2[i].w;
                if(dis2[v]>dis2[p]+w){
                    dis2[v]=dis2[p]+w;
                    if(!vis[v]){
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        } 
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1,x,y,z;i<=m;i++) scanf("%d%d%d",&x,&y,&z),add1(x,y,z),add2(y,x,z);
        spfa1();spfa2();
        for(int i=1;i<=n;i++){
            for(int j=head1[i];j;j=e1[j].next){
                f[++cnt]=dis1[i]+e1[j].w+dis2[e1[j].v];
            }
        }
        sort(f+1,f+cnt+1);
        for(int i=2;i<=cnt;i++) if(f[i]>f[1]&&f[i]<1061110972){printf("%d",f[i]);return 0;}
        printf("-1");
        return 0;
    }

    PS:

    本题做的是严格次短路

    对于次短路http://blog.csdn.net/u012288458/article/details/47264147

  • 相关阅读:
    cv2 Qt Platform plugin "cocoa" not found error
    开发scrapy web界面(一)
    java2smali python 粘合脚本
    react如何设置代理
    Nginx启动不了失败原因
    前端,后端,服务器如何部署,转载
    匿名函数普通函数和构造函数
    闭包的认识
    各种命名规范,打好基础才能建设高楼
    mongoose常用操作
  • 原文地址:https://www.cnblogs.com/shenben/p/5741741.html
Copyright © 2011-2022 走看看