题目描述

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水过,注意起点是s,不是1。我因为这个WA了一次

//Gang
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#define FOR(x,y,z) for(int x=y;x<=z;x++)
#define REP(x,y,z) for(int x=y;x>=z;x--)
#define ll long long
using namespace std;
int n,m,x;
];
],hd[];
],hd1[];
int cnt;
int a,b,t;
struct node
{
    int v,next,dis;
} e[],e1[];
void SPFA()
{
    queue<int>q;
    q.push(x);
    book[x]=;
    dis1[x]=;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        book[u]=;
        for(int i=hd[u];i;i=e[i].next)
        {
            int v=e[i].v;
            if(dis1[v]>dis1[u]+e[i].dis)
            {
                dis1[v]=dis1[u]+e[i].dis;
                if(!book[v])
                {
                    q.push(v);
                    book[v]=;
                }
            }
        }
    }
}
void SPFA2()
{
    queue<int>q;
    q.push(x);
    book[x]=;
    dis2[x]=;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        book[u]=;
        for(int i=hd1[u];i;i=e1[i].next)
        {
            int v=e1[i].v;
            if(dis2[v]>dis2[u]+e1[i].dis)
            {
                dis2[v]=dis2[u]+e1[i].dis;
                if(!book[v])
                {
                    q.push(v);
                    book[v]=;
                }
            }
        }
    }
}
int main()
{
    memset(dis1,0x7f,sizeof(dis1));
    memset(dis2,0x7f,sizeof(dis2));
    scanf("%d%d%d",&n,&m,&x);
    FOR(i,,m)
    {
        scanf("%d%d%d",&a,&b,&t);
        e[i].v=b;
        e[i].dis=t;
        e[i].next=hd[a];
        hd[a]=i;
        e1[i].v=a;
        e1[i].dis=t;
        e1[i].next=hd1[b];
        hd1[b]=i;
    }
    SPFA();
    memset(book,,sizeof(book));
    SPFA2();
    ;
    FOR(i,,n)
    min1=max(min1,dis1[i]+dis2[i]);
    printf("%d",min1);
    ;
}

洛谷银牛派对SPFA的更多相关文章

  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 题解

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

  3. 洛谷P1342 请柬(SPFA)

    To 洛谷.1342 请柬 题目描述 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院,尤其是古色古香的喜剧片.他们已经打印请帖和所有必要的信息和计 ...

  4. [洛谷P3697]开心派对小火车

    题目:洛谷P3697 题目大意是有各站停列车(慢车,相邻2站时间A)和特急列车(相邻2站时间B),特急列车在特定站点停靠. 现在加一种快速列车(相邻2站时间C,A>C>B),停靠K站(包括 ...

  5. [洛谷201704R1]开心派对小火车

    OJ题号:洛谷P3697 思路: 贪心.首先从起点出发,开特急电车,对于每一个特急车站$s_{i}$,分别下一次车,计算从当前车站$s_{i}$出发坐各停电车在指定时限内$t$最远能够到达的车站$r_ ...

  6. 洛谷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. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

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

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

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

  9. 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 ...

随机推荐

  1. python学习笔记(一)之入门

    1.python的安装 官网下载.exe文件直接安装即可,在安装过程中选择加入环境变量,就不用在安装后再去增添环境变量了. 本文选择的是python3.6版本,没有选择2.7版本. 2.启动pytho ...

  2. JAVA基础知识总结:五

    一.初步认识数组 1.理解数组 数组是用来存储相兼容数据类型的定长的容器 特点: a.只能存放相兼容数据类型,不能存放多种数据类型 b.可以存放基本数据类型和引用数据类型 c.数组是定长的,一旦被初始 ...

  3. poi入门之读写excel

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.该篇是介绍poi基本的 ...

  4. LeetCode 190. Reverse Bits (反转位)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  5. CTS/GTS 常见问题解答

    1.android.security.cts.PackageSignatureTest测试fail(MTK平台) 原因:由于用的是google default key而导致的fail,需要自己定义re ...

  6. 基于Nginx+redis的Asp.net站点搭建

    剧情介绍 在传统的信息系统(比如小规模的ERP\MES系统),往往只是进行简单的应用服务器和数据库服务器的分布式部署,以此来提高应用系统的负载能力,而伴随着访问的增大,应用服务器层面除了做硬件和网络的 ...

  7. python基础6 迭代器 生成器

    可迭代的:内部含有__iter__方法的数据类型叫可迭代的,也叫迭代对象实现了迭代协议的对象 运用dir()方法来测试一个数据类型是不是可迭代的的. 迭代器协议是指:对象需要提供next方法,它要么返 ...

  8. C++获取基类指针所指子类对象的类名

    我们在程序中定义了一个基类,该基类有n个子类,为了方便,我们经常定义一个基类的指针数组,数组中的每一项指向都指向一个子类,那么在程序中我们如何判断这些基类指针是指向哪个子类呢? 关键字 typeid, ...

  9. 补写:Best Coder #85 1001 Sum(前缀和)

    sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...

  10. 有关CUBLAS中的矩阵乘法函数

    关于cuBLAS库中矩阵乘法相关的函数及其输入输出进行详细讨论. ▶ 涨姿势: ● cuBLAS中能用于运算矩阵乘法的函数有4个,分别是 cublasSgemm(单精度实数).cublasDgemm( ...