首页 > 其他分享 >ACM International Collegiate Programming Contest 2014 B SPFA 统计路径

ACM International Collegiate Programming Contest 2014 B SPFA 统计路径

时间:2023-04-24 22:37:33浏览次数:37  
标签:entrance Contest int trails point Programming metres ACM peak


Flowery Trails
!”

()”

*+,-).%”
/)’0”
122”
1!2”
342”
522”
!22”
652”
!42”
72” 72”
5!2”
!12” 622”
18!” 162”
!12”
6”
7”
4”
9”
3”
5”
8”
1”
2”
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).
Task
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 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.
Universidade do Porto Computer Science Department 5
Problem B Problem B
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
The output has a single line with the extent of flowers (in metres) needed to cover both
sides of the popular trails.
Sample Input 1
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
Sample Output 1
3860
Sample Input 2
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 2
18

思路:先从s->t 跑一遍spfa,再从 t->s 跑一遍,分别求出距离长度;
接着:易知 如果某一点 m,s->m 的最短距离+m->m.to的距离+m.to->t==最短路长度,那么这一段就在最短路上面。

#include<bits/stdc++.h>
using namespace std;
#define maxn 500005
#define inf 0x3f3f3f3f
typedef long long ll;
struct edge
{
    int to,w,next;
 }e[maxn];
 int head[maxn],vis[maxn];
 int d1[maxn],d2[maxn];
 int n,m ,t;
 //ll ans;
 void add(int i,int j,int w)
 {
 // e[t].from=i;
    e[t].to=j;
    e[t].w=w;
    e[t].next=head[i];
    head[i]=t++;
  }

void spfa(int s,int t ,int d[])
  {
    queue<int> q;
    for(int i=0;i<n;i++){
        d[i]=inf;
      }
      memset(vis,false,sizeof(vis));
      q.push(s);
      d[s]=0;
      vis[s]=true;
      while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(d[v]>d[u]+e[i].w){
                d[v]=d[u]+e[i].w;
                if(!vis[v]){
                    vis[v]=true;
                    q.push(v);
                  }
              }
          }
      }
  }

void sol()
{
    int s=0,t=n-1;
   ll ans=0;
    spfa(s,t,d1);
    spfa(t,s,d2);
    ll minn=d1[t];
    for(int u=0;u<n;u++){
        for(int i=head[u];~i;i=e[i].next){
            ll v=e[i].to;
            ll weight=e[i].w;
            if(d1[u]+d2[v]+weight==minn){
                ans+=weight;
            }
        }
    }
    cout<<ans*2<<endl;
}

int main()
{
    //int n,m;
    while(cin>>n>>m){
        memset(head,-1,sizeof(head));
        t=0;
        for(int i=0;i<m;i++){
            int u,v,l;
            cin>>u>>v>>l;
            add(u,v,l);
            add(v,u,l);
        }
        sol();
    }
}


标签:entrance,Contest,int,trails,point,Programming,metres,ACM,peak
From: https://blog.51cto.com/u_15657999/6221934

相关文章

  • ACM International Collegiate Programming Contest 2014 A dfs 好题
    GREAT+SWERC=PORTOWewanttohaveagreatSWERCatPortothisyearandweapproachedthischallengeinseveralways.Weevenframeditasawordadditionproblem,similartotheclassicSEND+MORE=MONEY,whereeachletterstandsforasingledigit(......
  • [CMU 15-418] (Lecture4) Parallel Programming Basics
    本系列文章为CMU15-418/15-618:ParallelComputerArchitectureandProgramming,Fall2018课程学习笔记课程官网:CMU15-418/15-618:ParallelComputerArchitectureandProgramming参考文章:CMU15-418notes相关资源与介绍:CMU15-418/StanfordCS149:ParallelComput......
  • AtCoder Beginner Contest 158
    AtCoderBeginnerContest158https://atcoder.jp/contests/abc158基础不牢,地动山摇D-StringFormation一个小小的STL应用#include<bits/stdc++.h>#definelllonglongusingnamespacestd;strings;intq,t,f;charc;intmain(){cin>>s>>q......
  • codeforces 414B B. Mashmokh and ACM(dp)
    题目链接:codeforces414B题目大意:定义一个序列,前一项能够整除后一项,给定这个序列中数的取值范围和序列的长度,问有多少种构造方法。题目分析:我们定义状态dp[i][j]为前i项已经确定且第i项为j的方案数。转移方程dp[i][j]=∑k|jdp[i−1][k]复杂度O(n⋅k)AC代码:#include<iostream>......
  • AtCoder Regular Contest 111 F Do you like query problems?
    洛谷传送门AtCoder传送门挺有意思的计数。计数感觉很难做,不妨转成期望,期望又可以转成概率之和。考虑枚举\(w\in[0,m-1]\),把\(>w\)的数设为\(1\),\(\lew\)的数设为\(0\)。那么期望就是所有\(w\),\(a_i\)为\(1\)的概率之和。对于一个\(i\),只有以下的操作能改变\(......
  • SMU Spring 2023 Trial Contest Round 9
    SMUSpring2023TrialContestRound9A-WrongSubtraction#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=1e5+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedefl......
  • SMU Spring 2023 Trial Contest Round 9
    A.WrongSubtraction#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){intn,k;cin>>n>>k;while(k--){if(n%10==0)n/=10;elsen--;}cout<<n;return0;}B.T......
  • Atcoder Beginner Contest 299 G
    对于要打印的\(B\),我们首先尝试确定\(B_1\)。让\(f(x)(1≤x≤M)\)是最大的\(i\),使\(A_i=x\)。对于\(r:=\underset{{{1≤x≤M}}}{\min}f(x)\),我们可以证明\(B_1\)是\(A_1,A_2,...,A_r\)中的一个(否则,\(B\)是\(A_{>r}:=(A_r+1,Ar+2,...,A_N)\)的一个子序列,但......
  • AtCoder Beginner Contest 283 Ex Popcount Sum
    洛谷传送门AtCoder传送门记录一下这个神奇的套路。首先有\(\operatorname{popcount}(n)=n-\sum\limits_{i=1}^{\infty}\left\lfloor\frac{n}{2^i}\right\rfloor\)。证一下:\[\operatorname{popcount}(n)=\sum\limits_{i=0}^{\infty}\left\lfloor\dfrac{n}{2^i}\right......
  • AtCoder Regular Contest 115 D Odd Degree
    洛谷传送门AtCoder传送门若连通块是一棵树,考虑钦定\(k\)个点为奇度点,方案数为\(\binom{n}{k}\)。对于叶子,如果它是奇度点,那么连向它父亲的边要保留,否则不保留。这样自底向上考虑,任意一条边的保留情况都可以唯一确定,所以最后方案数就是\(\binom{n}{k}\)。若连通块存在环,仍......