zoukankan      html  css  js  c++  java
  • 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

    Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.

    Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

    寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

    每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

    输入输出格式

    输入格式:
    第一行三个整数N,M, X;

    第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

    输出格式:
    一个整数,表示最长的最短路得长度。

    输入输出样例

    输入样例#1:
    4 8 2
    1 2 4
    1 3 2
    1 4 7
    2 1 1
    2 3 5
    3 1 2
    3 4 4
    4 2 3
    输出样例#1:
    10

    说明

    这里写图片描述

    题解

    很经典的最短路,虽然数据不坑

    思路:这题是一个单源最短路和单终点最短路,前者大家都会,一遍SPFA或Dijkstra就行了(数据也没卡SPFA);可是后者单终点最短路怎么办呢?可以把边反过来(具体可以在输入时就建两个图,一正一反),因为单源和单终点只是方向改变,所以很容易发现这么做是对的。

    具体做法:很简单呢,两遍SPFA,不过要写两个函数。

    注意:好像没什么可以注意的。。。


    接下来贴代码:

    #include<bits/stdc++.h>
    #define inf 2000000000
    using namespace std;
    struct edge{
        int to,next,w;
    }e[100001],e2[100001];
    int n,m,s;
    int head[1001],head2[1001];
    int in[1001];
    int d1[1001];
    int d2[1001];
    
    void spfa(){
        queue<int> q;
        q.push(s);
        in[s]=1;
        d1[s]=0;
        while(!q.empty()){
            int t=q.front();
            q.pop();
            in[t]=0;
            for(int i=head[t];i!=0;i=e[i].next){
                if(d1[t]+e[i].w<d1[e[i].to]){
                    d1[e[i].to]=d1[t]+e[i].w;
                    if(!in[e[i].to]){
                        in[e[i].to]=1;
                        q.push(e[i].to);
                    }
                }
            }
        }
    }
    void spfa2(){
        queue<int> q;
        q.push(s);
        in[s]=1;
        d2[s]=0;
        while(!q.empty()){
            int t=q.front();
            q.pop();
            in[t]=0;
            for(int i=head2[t];i!=0;i=e2[i].next){
                if(d2[t]+e2[i].w<d2[e2[i].to]){
                    d2[e2[i].to]=d2[t]+e2[i].w;
                    if(!in[e2[i].to]){
                        in[e2[i].to]=1;
                        q.push(e2[i].to);
                    }
                }
            }
        }
    }
    int main(){
        cin>>n>>m>>s;
        for(int i=1;i<=n;i++){
            d1[i]=inf;
            d2[i]=inf;
        }
        for(int i=1;i<=m;i++){
            int a,b,l;
            scanf("%d %d %d",&a,&b,&l);
            e[i].to=b;
            e[i].next=head[a];
            head[a]=i;
            e[i].w=l;
            e2[i].to=a;
            e2[i].next=head2[b];
            e2[i].w=l;
            head2[b]=i;
        }
        spfa();
        memset(in,0,sizeof(in));
        spfa2();
        int mx=0;
        for(int i=1;i<=n;i++){
            mx=max(mx,d1[i]+d2[i]);
        }
        cout<<mx;
        return 0;
    }
  • 相关阅读:
    使用ngx_lua构建高并发应用(1)
    nginx+lua项目学习
    学习乱
    if---(switch-case)语句初步学习总结
    数据类型转换
    总结:C#变量,占位符等相关知识
    学习随笔
    开始我的.NET的学习旅程
    Python 网络爬虫 008 (编程) 通过ID索引号遍历目标网页里链接的所有网页
    Python 网络爬虫 007 (编程) 通过网站地图爬取目标站点的所有网页
  • 原文地址:https://www.cnblogs.com/stone41123/p/7581308.html
Copyright © 2011-2022 走看看