zoukankan      html  css  js  c++  java
  • UVALive 6885 Flowery Trails 最短路

    Flowery Trails

    题目连接:

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4897

    Description

    In order to attract more visitors, the manager of a national
    park had the idea of planting flowers along both
    sides of the popular trails, which are the trails used by
    common people. Common people only go from the park
    entrance to its highest peak, where views are breathtaking,
    by a shortest path. So, he wants to know how many
    metres of flowers are needed to materialize his idea.
    For instance, in the park whose map is depicted in
    the figure, common people make only one of the three
    following paths (which are the shortest paths from the
    entrance to the highest peak).
    • At the entrance, some start in the rightmost trail
    to reach the point of interest 3 (after 100 metres),
    follow directly to point 7 (200 metres) and then pick
    the direct trail to the highest peak (620 metres).
    • The others go to the left at the entrance and reach
    point 1 (after 580 metres). Then, they take one of
    the two trails that lead to point 4 (both have 90
    metres). At point 4, they follow the direct trail to the highest peak (250 metres).
    Notice that popular trails (i.e., the trails followed by common people) are highlighted in yellow. Since
    the sum of their lengths is 1930 metres, the extent of flowers needed to cover both sides of the popular
    trails is 3860 metres (3860 = 2 × 1930).
    Given a description of the park, with its points of interest and (two-way) trails, the goal is to find
    out the extent of flowers needed to cover both sides of the popular trails. It is guaranteed that, for the
    given inputs, there is some path from the park entrance to the highest peak.

    Input

    The input file contains several test cases, each of them as described below.
    The first line of the input has two integers: P and T. P is the number of points of interest and T
    is the number of trails. Points are identified by integers, ranging from 0 to P − 1. The entrance point
    is 0 and the highest peak is point P − 1.
    Each of the following T lines characterises a different trail. It contains three integers, p1, p2, and
    l, which indicate that the (two-way) trail links directly points p1 and p2 (not necessarily distinct) and
    has length l (in metres).
    Integers in the same line are separated by a single space.
    Constraints:
    2 ≤ P ≤ 10 000 Number of points.
    1 ≤ T ≤ 250 000 Number of trails.
    1 ≤ l ≤ 1 000 Length of a trail

    Output

    For each test case, the output has a single line with the extent of flowers (in metres) needed to cover
    both sides of the popular trails.

    Sample Input

    10 15
    0 1 580
    1 4 90
    1 4 90
    4 9 250
    4 2 510
    2 7 600
    7 3 200
    3 3 380
    3 0 150
    0 3 100
    7 8 500
    7 9 620
    9 6 510
    6 5 145
    5 9 160
    4 7
    0 1 1
    0 2 2
    0 3 10
    0 3 3
    1 3 2
    2 3 1
    1 1 1

    Sample Output

    3860
    18

    Hint

    题意

    求在最短路上的边的长度和

    题解:

    枚举边,如果边起点到一端的距离+终点到一端的距离+这条边的长度,那么这条边就在最短路上。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 10005;
    const int Maxn = 250005;
    int d1[maxn],d2[maxn];
    int n,m;
    struct node{
        int x,y;
        node(int X,int Y):x(X),y(Y){};
    };
    vector<node> E[maxn];
    int a[Maxn],b[Maxn],c[Maxn];
    priority_queue<pair<int,int> >Q;
    void init(){
        while(!Q.empty())Q.pop();
        for(int i=0;i<maxn;i++)E[i].clear();
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
            E[a[i]].push_back(node{b[i],c[i]});
            E[b[i]].push_back(node(a[i],c[i]));
        }
        for(int i=0;i<maxn;i++)d1[i]=1000000005;
        for(int i=0;i<maxn;i++)d2[i]=1000000005;
    
        Q.push(make_pair(0,0));
        d1[0]=0;
        while(!Q.empty()){
            int now=Q.top().second;
            Q.pop();
            for(int i=0;i<E[now].size();i++){
                int v=E[now][i].x;
                int sp=E[now][i].y;
                if(d1[v]>d1[now]+sp){
                    d1[v]=d1[now]+sp;
                    Q.push(make_pair(-d1[v],v));
                }
            }
        }
    
        Q.push(make_pair(0,n-1));
        d2[n-1]=0;
        while(!Q.empty()){
            int now=Q.top().second;
            Q.pop();
            for(int i=0;i<E[now].size();i++){
                int v=E[now][i].x;
                int sp=E[now][i].y;
                if(d2[v]>d2[now]+sp){
                    d2[v]=d2[now]+sp;
                    Q.push(make_pair(-d2[v],v));
                }
            }
        }
        long long ans = 0;
        for(int i=1;i<=m;i++){
            if(d1[n-1]==d1[a[i]]+d2[b[i]]+c[i])
                ans+=2ll*c[i];
            else if(d1[n-1]==d2[a[i]]+d1[b[i]]+c[i])
                ans+=2ll*c[i];
        }
        cout<<ans<<endl;
        }
    }
  • 相关阅读:
    sqlserver建立临时表
    动态引用WebService
    技术的力量:30分钟的动画片和《彗星撞地球》超炫的动画 仅64K
    sqlserver2005新功能函数
    使用面向对象的、完整的单点登录功能
    asp.net上传功能(单文件,多文件,自定义生成缩略图,水印)
    C#对字符和文件的加密解密类
    JavaScript中setInterval函数应用常见问题之一(第一个参数不加引号与加引号的区别)
    JavaScript表格隔行换色悬停高亮
    Javascript模拟c#中arraylist操作(学习分享)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5677476.html
Copyright © 2011-2022 走看看