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. 边双连通缩点+树dp 2015 ACM Arabella Collegiate Programming Contest的Gym - 100676H

    http://codeforces.com/gym/100676/attachments 题目大意: 有n个城市,有m条路,每条路都有边长,如果某几个城市的路能组成一个环,那么在环中的这些城市就有传送 ...

  2. java绝对路径和相对路径的理解

    日常开发中引用东西经常会遇到路径问题,各种尝试,各种出错,其实只要理解了这两种路径,问题便迎刃而解. 在java中路径有两种表示方法:绝对路径和相对路径. (1) 相对路径:它以不带“\”的目录名表示 ...

  3. Java学习遇到的问题

    一. Java中泛型如何比较大小,继承Comparable类,然后实现其唯一的方法compareTo(): public class GenericClass<E extends Compara ...

  4. 您是哪个等级的CSS开发人员?

    我们在不断的学习,追求进步与提高,到底学到什么程度了,到底是 不是真的了解CSS,是哪个层次了呢.我们来对照一下. 第0级:CSS?那不是一个多人射击游戏吗?  CSS? Isn't that a m ...

  5. GD库imagecopyresampled()方法详解~

    整理了一下GD库这个缩放,拉伸复制的方法 因为这个函数参数太多了~ imagecopyresampled()   /* //拷贝部分图像并调整大小 bool imagecopyresampled ( ...

  6. 彻底解决mysql中文乱码

    mysql是我们项目中非常常用的数据型数据库.但是因为我们需要在数据库保存中文字符,所以经常遇到数据库乱码情况.下面就来介绍一下如何彻底解决数据库中文乱码情况. 1.中文乱码 1.1.中文乱码 cre ...

  7. 2018中国科大自主测试-B卷部分试题

    数学部分 z = e^{\frac{2i\pi}{3}}, 求z^{2018}. \sin(2x) = \frac 35, 求\frac{\tan(x+15^{\circ})}{\tan(x-15^{ ...

  8. 向量与矩阵的范数及其在matlab中的用法(norm)

    一.常数向量范数 \(L_0\) 范数 \(\Vert x \Vert _0\overset{def}=\)向量中非零元素的个数 其在matlab中的用法: sum( x(:) ~= 0 ) \(L_ ...

  9. think php模板的使用

    {include file="../application/public/header.html"}<!-- Jumbotron --><div class=&q ...

  10. 集合框架之Map学习

    Map接口的实现类有HashTable.HashMap.TreeMap等,文章学习整理了“ Map和HashMap的使用方法”. /** * Map和HashMap的使用方法 */public sta ...