洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

Posted z360

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party相关的知识,希望对你有一定的参考价值。

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跑最长路,再求起点到其他点的最短路的时候可以不跑n边spfa,而转为建反向边,然后在跑一遍spfa就好了

这个题跟我们以前做过的一道题很像——洛谷:邮递员送信        https://www.luogu.org/problemnew/show/1629

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100000
#define maxn 99999999
using namespace std;
long long ans;
int n,m,e,x,y,z,s,tot,dis[N],head[N],dis1[N],dis2[N];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();}
    return x*f;
}
struct NN
{
    int x,y,z;
}edde[N];
struct Edge
{
    int to,dis,from,next;
}edge[N];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int spfa(int s)
{
    queue<int>q;  bool vis[N]; int sum=0;
    for(int i=1;i<=n;i++) dis[i]=maxn,vis[i]=false;
    q.push(s);vis[s]=true;dis[s]=0;
    while(!q.empty())
    {
        int x=q.front(); q.pop();
        for(int i=head[x];i;i=edge[i].next)
        {
            int t=edge[i].to;
            if(dis[t]>dis[x]+edge[i].dis)
            {
                dis[t]=dis[x]+edge[i].dis;
                if(!vis[t])
                {
                    q.push(t);
                    vis[t]=true;
                }
            }
        }
        vis[x]=false;
    }
}
int main()
{
    n=read(),m=read();e=read();
    for(int i=1;i<=m;i++)
    {
        x=read(),y=read(),z=read();
        add(x,y,z);
        edde[i].x=x;edde[i].y=y,edde[i].z=z;
    }
    spfa(e);
    for(int i=1;i<=n;i++)
     dis1[i]=dis[i];
    s=tot,tot=0;
    memset(dis,0,sizeof(dis));
    memset(head,0,sizeof(head));
    memset(edge,0,sizeof(edge));
    for(int i=1;i<=s;i++)
      add(edde[i].y,edde[i].x,edde[i].z);
    spfa(e);
    for(int i=1;i<=n;i++)
     dis2[i]=dis[i];
    for(int i=1;i<=n;i++)
     if(dis1[i]+dis2[i]>ans)
      ans=dis1[i]+dis2[i];
    printf("%lld",ans);
    return 0;
}

 

以上是关于洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party的主要内容,如果未能解决你的问题,请参考以下文章

Luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party

luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon

P1884 [USACO12FEB]过度种植(银)

P4824 [USACO15FEB]Censoring (Silver) 审查(银)