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,两边的dis值相加求最大值。

    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 100100
    using namespace std;
    int n,m,x,tot,tot1;
    int dis[MAXN],dis1[MAXN],vis[MAXN];
    int to[MAXN],net[MAXN],cap[MAXN],head[MAXN];
    int to1[MAXN],net1[MAXN],cap1[MAXN],head1[MAXN];
    void add(int u,int v,int w){
        to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot;
    }
    void add1(int u,int v,int w){
        to1[++tot1]=v;net1[tot1]=head1[u];cap1[tot1]=w;head1[u]=tot1;
    }
    void spfa(int s){
        queue<int>que;
        memset(vis,0,sizeof(vis));
        memset(dis,0x3f,sizeof(dis));
        que.push(s);dis[s]=0;vis[s]=1;
        while(!que.empty()){
            int now=que.front();
            que.pop();vis[now]=0;
            for(int i=head[now];i;i=net[i])
                if(dis[to[i]]>dis[now]+cap[i]){
                    dis[to[i]]=dis[now]+cap[i];
                    if(!vis[to[i]]){
                        vis[to[i]]=1;
                        que.push(to[i]);
                    }
                }
        }
    }
    void spfa1(int s){
        queue<int>que;
        memset(vis,0,sizeof(vis));
        memset(dis1,0x3f,sizeof(dis1));
        que.push(s);dis1[s]=0;vis[s]=1;
        while(!que.empty()){
            int now=que.front();
            que.pop();vis[now]=0;
            for(int i=head1[now];i;i=net1[i])
                if(dis1[to1[i]]>dis1[now]+cap1[i]){
                    dis1[to1[i]]=dis1[now]+cap1[i];
                    if(!vis[to1[i]]){
                        vis[to1[i]]=1;
                        que.push(to1[i]);
                    }
                }
        }
    }
    int main(){
        scanf("%d%d%d",&n,&m,&x);
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add1(v,u,w);
        }
        spfa(x);spfa1(x);int maxn=-1;
        for(int i=1;i<=n;i++)
            if(dis[i]+dis1[i]>maxn&&i!=x)    maxn=dis[i]+dis1[i];
        cout<<maxn;
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    110、抽象基类为什么不能创建对象?
    109、什么情况会自动生成默认构造函数?
    108、如果想将某个类用作基类,为什么该类必须定义而非声明?
    107、类如何实现只能静态分配和只能动态分配
    106、C++中的指针参数传递和引用参数传递有什么区别?底层原理你知道吗?
    hdoj--2036--改革春风吹满地(数学几何)
    nyoj--46--最少乘法次数(数学+技巧)
    vijos--P1211--生日日数(纯模拟)
    nyoj--42--一笔画问题(并查集)
    nyoj--49--开心的小明(背包)
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8413096.html
Copyright © 2011-2022 走看看