zoukankan      html  css  js  c++  java
  • 315. [POJ3255] 地砖RoadBlocks

    ★★★   输入文件:block.in   输出文件:block.out   简单对比

    时间限制:1 s   内存限制:128 MB

    Description

    Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

    The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

    The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

    贝茜搬到了一个小农场去住,但她很享受偶尔回去看看她的一个好朋友。贝茜很喜欢沿途的风景,因此她希望路上走的尽可能慢一些。于是她决定走次短路而不是最短路。

    城郊包含R(1<=R<=100,000)条双向的路,它们分别连接了N(1<=N<=5000)个十字路口(节点),为了方便我们把它们标记为1..N。贝茜从1号节点出发,她的好朋友(目的地)在N号节点。

    次短路和最短路可以部分重合,次短路也有可能经过同一条路或同一个节点多次。次短路是长于最短路的路中最短的那一条。当有多个最短路存在时,次短路是其他所有路中最短的那条。

    Input

    Line 1: Two space-separated integers: N and R 

    Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

    输入:

    第1行:两个被空格分开的整数N和R

    第2..R+1行:每行包括三个由空格分开的整数A,B和D组成。用来描述连接节点A和节点B之间路径的长度是D(1<=D<=5000)。

    Output

    Line 1: The length of the second shortest path between node 1 and node N

    输出:

    第一行:一个整数表示从节点1到节点N之间次短路的长度。

    Sample Input

    4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100

    Sample Output

    450

    Hint

    Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

    提示:

    样例中由两条从节点1到节点N的路分别是1->2->4(len=100+200=300(最短路))和1->2->3->4(100+250+100=450(次短路))。

    译byKZFFFFFFFF

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<cstdlib>
     
    using namespace std;
    const int N=200010;
    const int INF=99999999;
     
    int n,m,now1=1,js;
    int head[N>>2],dis[N>>2];
    bool vis[N>>2];
    struct node{
        int u,v,w,nxt;
    }E[N];
    struct Node{
        int start,noww,will;
    }now,ttop,nxt;
     
    inline int read()
    {
        int x=0;char c=getchar();
        while(c<'0'||c>'9')c=getchar();
        while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
        return x;
    }
     
    inline void add(int u,int v,int w)
    {
        E[now1].v=v;
        E[now1].w=w;
        E[now1].nxt=head[u];
        head[u]=now1++;
    }
     
    bool operator < (Node a,Node b)
    {return a.will>b.will;}
     
    inline void spfa(int start)
    {
        queue<int>q;
        for(int i=1;i<=n;i++)
            dis[i]=INF;
        dis[start]=0;
        vis[start]=1;
        q.push(start);
        while(!q.empty())
        {
            int topp=q.front();
            q.pop();
            vis[topp]=0;
            for(int i=head[topp];~i;i=E[i].nxt)
            {
                if(dis[E[i].v]>dis[topp]+E[i].w)
                {
                    dis[E[i].v]=dis[topp]+E[i].w;
                    if(!vis[E[i].v])
                    {
                        vis[E[i].v]=1;
                        q.push(E[i].v);
                    }
                }
            }
        }
    }
     
    inline void Astar(int start,int endd)
    {
        if(dis[start]==INF)return ;
        now.start=start;now.noww=0;now.will=dis[start];
        priority_queue<Node>q;
        q.push(now);
        while(!q.empty())
        {
            ttop=q.top();
            q.pop();
            if(ttop.start==endd)
            {
                js++;
                if(js==2)
                {
                    printf("%d",ttop.noww);
                    exit(0);
                }
            }
            for(int i=head[ttop.start];~i;i=E[i].nxt)
            {
                nxt.start=E[i].v;
                nxt.noww=ttop.noww+E[i].w;
                nxt.will=nxt.noww+dis[E[i].v];
                q.push(nxt);
            }
        }
    }
     
    int main()
    {
        freopen("block.in","r",stdin);
        freopen("block.out","w",stdout);
        n=read(),m=read();
        for(int i=1;i<=n;i++)
            head[i]=-1;
        for(int i=1;i<=m;i++)
        {
            int u=read(),v=read(),w=read();
            add(u,v,w);
            add(v,u,w);
        }
        spfa(n);
        Astar(1,n);
        return 0;
    }
    

      

  • 相关阅读:
    extjs 网站首页table布局,秀一下
    asp.net中应用Extjs的grid不显示HTML内容
    EXTjs 同时支持文件上传和图片上传的htmleditor
    在给Ext2 Grid设置了autoHeight属性后,如何显示滚动条
    Extjs 扩展Htmleditor,支持 图片上传 文件上传 插入flash 插入多媒体 插入层 插入横线等功能
    javascritp【1】学习
    sql连接查询
    Div+CSS布局居中
    <%=CutStr(trim(rs("p_name")),1,8)%> CutStr函数参数说明
    图片提交按钮和重复提交表单
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7154143.html
Copyright © 2011-2022 走看看