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()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; 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<;
    ;i<=n;i++) dis[i]=maxn,vis[i]=false;
    q.push(s);vis[s]=;
    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();
    ;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);
    ;i<=n;i++)
     dis1[i]=dis[i];
    s=tot,tot=;
    memset(dis,,sizeof(dis));
    memset(head,,sizeof(head));
    memset(edge,,sizeof(edge));
    ;i<=s;i++)
      add(edde[i].y,edde[i].x,edde[i].z);
    spfa(e);
    ;i<=n;i++)
     dis2[i]=dis[i];
    ;i<=n;i++)
     if(dis1[i]+dis2[i]>ans)
      ans=dis1[i]+dis2[i];
    printf("%lld",ans);
    ;
}

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party的更多相关文章

  1. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

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

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

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

    银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...

  4. 洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party

    [题解] 其实解法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long l ...

  5. P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

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

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  7. 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...

  8. [USACO07FEB]银牛派对Silver Cow Party

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

  9. 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

    更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...

随机推荐

  1. uva 11971 Polygon

    https://vjudge.net/problem/UVA-11971 有一根长度为n的木条,随机选k个位置把它们切成k+1段小木条.求这些小木条能组成一个多边形的概率. 将木条看做一个圆,线上切k ...

  2. 最小生成树的边的概念问题!!! 最小生成树的计数 bzoj 1016

    1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5292  Solved: 2163[Submit][St ...

  3. HDU 2239 polya计数 欧拉函数

    这题模数是9937还不是素数,求逆元还得手动求. 项链翻转一样的算一种相当于就是一种类型的置换,那么在n长度内,对于每个i其循环节数为(i,n),但是由于n<=2^32,肯定不能直接枚举,所有考 ...

  4. Yii2 的 redis 应用

    在应用的时候需要先对yii2进行扩展安装 如果装有composer直接运行 php composer.phar require --prefer-dist yiisoft/yii2-redis 当然也 ...

  5. 20、什么样的项目适合Web自动化测试

    1.什么是Web自动化测试?概念:让程序代替人为自动验证Web项目功能的过程 2.什么Web项目适合做自动化测试 1.需求变动不频繁 2.项目周期长 3.项目需要回归测试 3.如阿进行Web自动化测试 ...

  6. PHP对象2: 构造函数与析构函数

    当一个对象的所有引用都没有时, 一个对象才消失, 这时才执行析构函数 <?php class firecat{ public $name; function say(){ echo 'I lov ...

  7. python3-可变和不可变数据类型

    可变:[ ]    { } 不可变:int    str   ( )     应用实例: 把列表l,追加到列表s中,现在网列表l中追加一个5,打印列表s可以看到,列表s中的列表l中也有5. d={&q ...

  8. 认识Cookie和状态管理

    HTTP协议是一种无状态的协议,WEB服务器本身不能识别出哪些请求是同一个浏览器发出的 ,浏览器的每一次请求都是完全孤立的 即使 HTTP1.1 支持持续连接,但当用户有一段时间没有提交请求,连接也会 ...

  9. Spring源码解读Spring IOC原理

    一.什么是Ioc/DI? IoC 容器:最主要是完成了完成对象的创建和依赖的管理注入等等. 先从我们自己设计这样一个视角来考虑: 所谓控制反转,就是把原先我们代码里面需要实现的对象创建.依赖的代码,反 ...

  10. 在C#中用MediaInfo获取视频或音频的属性

    MediaInfo是一个开源的获取视频或音频的信息的非常便利的工具,它本身就带有一个GUI界面,可以非常方便我们查看视频信息.但是,当我们写一些转码程序时,往往需要在程序中获取视频信息的时候. 以前我 ...