zoukankan      html  css  js  c++  java
  • POJ-3255-Roadblocks(次短路的另一种求法)

    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).

    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)

    Output

    Line 1: The length of the second shortest path between node 1 and node 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)
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    
    struct node
    {
       int pos,w;
       node(int x,int y)
       {
           pos=x;
           w=y;
       }
       bool friend operator<(node x,node y )
       {
           return x.w>y.w;
       }
    };
    struct edge
    {
       int u,v;
       ll cost;
       int nxt;
    }Edge[maxn<<1];
    int cnt;
    
    ll dis[5005],dis2[5005];
    int head[5005],vis[5005];
    void Add(int u,int v,ll w)
    {
       Edge[cnt].u=u;
       Edge[cnt].v=v;
       Edge[cnt].cost=w;
       Edge[cnt].nxt=head[u];
       head[u]=cnt++;
    }
    void Dijkstra(int u)
    {
        dis[u]=0;
        priority_queue<node>q;
        q.push(node(u,0));
        while(!q.empty())
        {
           node now=q.top();
           q.pop();
           if(vis[now.pos])
           {
               continue;
           }
           vis[now.pos]=1;
           for(int t=head[now.pos];t!=-1;t=Edge[t].nxt)
           {
               if(dis[now.pos]+Edge[t].cost<dis[Edge[t].v])
               {
                   dis[Edge[t].v]=dis[now.pos]+Edge[t].cost;
                   q.push(node(Edge[t].v,dis[Edge[t].v]));
               }
           }
        }
        return ;
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(dis,0x3f3f3f3f,sizeof(dis));
        cnt=0;
        int u,v;
        ll w;
        for(int t=0;t<m;t++)
        {
            scanf("%d%d%lld",&u,&v,&w);
            Add(u,v,w);
            Add(v,u,w);
        }
        Dijkstra(1);
        for(int t=1;t<=n;t++)
        {
            dis2[t]=dis[t];
        }
        memset(dis,0x3f3f3f3f,sizeof(dis));
        memset(vis,0,sizeof(vis));
        Dijkstra(n);
        int ans=0x3f3f3f3f;
        for(int t=0;t<cnt;t++)
        {
            if(dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost<ans&&(dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost)!=dis2[n])
            {
                ans=dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost;
            }
        }
        printf("%d
    ",ans);
        system("pause");
        return 0;
    }
    
    
  • 相关阅读:
    Content-Type: multipart/form-data; boundary=
    -ErrorAction SilentlyContinue
    計量文件夾大小
    使用tesseract識別簡單的圖形碼
    适用于演示场景的修改网页内容
    分页展示与标题行
    查询当前脚本运行环境
    选项菜单
    tomcat创建虚拟目录存储图片
    springmvc记录(页面回显、url模板映射、自定义参数绑定、全局异常处理、文件上传、拦截器、hibernate validation数据校验、静态资源映射)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11337315.html
Copyright © 2011-2022 走看看