题目描述

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就好啦。

代码:

/*
Name: luogu 1821 Silver Cow Party
Author: Manjusaka
Date: 18-07-14 14:25
*/ #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define N int(1e3+2)
#define M int(1e5+2)
int a[M],b[M],c[M];
int n,m,s,ans;
int head[N],tot;
struct ahah{
int nxt,to,dis;
}edge[M];
int d[N],dd[N];
void add(int x,int y,int z)
{
edge[++tot].nxt=head[x],edge[tot].to=y,edge[tot].dis=z,head[x]=tot;
}
bool vis[N];
queue <int> que;
void spfa(int s)
{
for(int i=;i<=n;i++)d[i]=0x7fffff;
vis[s]=;que.push(s);d[s]=;
while(!que.empty())
{
int temp=que.front();
vis[temp]=; que.pop();
for(int i=head[temp];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(d[v]>d[temp]+edge[i].dis)
{
d[v]=d[temp]+edge[i].dis;
if(!vis[v])
{
vis[v]=;
que.push(v);
}
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
add(a[i],b[i],c[i]);
}
spfa(s);
for(int i=;i<=n;i++)dd[i]=d[i];
tot=;
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
for(int i=;i<=m;i++)add(b[i],a[i],c[i]);
spfa(s);
for(int i=;i<=n;i++)
{
if(d[i]+dd[i]>ans)ans=d[i]+dd[i];
}
printf("%d",ans);
}

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

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

  3. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

  4. Silver Cow Party(最短路,好题)

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  5. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  7. poj 3268 Silver Cow Party

                                                                                                       S ...

  8. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  9. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

随机推荐

  1. 图像分类与KNN

    1 图像分类问题 1.1 什么是图像分类 所谓图像分类问题,就是已有固定的分类标签集合,然后对于输入的图像,从分类标签集合中找出一个分类标签,最后把分类标签分配给该输入图像.虽然看起来挺简单的,但这可 ...

  2. Bid和Ask

    一直很懵到底哪个是哪个,记吧,很快就又懵了.网上又坑,每一个解释清楚的.这次搞明白了记下来. 当然,这么逗比的取名法我也是醉了.直接加点东西,UserBuy,UserSell,BankBuy,Bank ...

  3. UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...

  4. shell Syntax error: Bad fd number 错误解决

    最近在玩spark , 需要看一下python的spark lib 是怎么加入环境变量的. 执行: sh -x bin/pyspark 报错 + dirname bin/pyspark + cd bi ...

  5. Fiddler 学习

    Little Skill Fiddler 官网: https://www.telerik.com/fiddler 来自 https://www.cnblogs.com/zhaoyanjun/p/706 ...

  6. 用CSS绘制三角形

    其实用HTML CSS绘制三角行 是非常简单的 ,我在网上看了不少人写的博客,里面写的好复杂样子,反正我是看的云里雾里的,说实话是挺简单的. 首先提出一段代码: <!DOCTYPE html&g ...

  7. Python之单元测试——HTMLTestRunner

    前置条件:把HTMLTestRunner.py文件拷贝到External Libraries—>site-packages里面 import unittestimport HTMLTestRun ...

  8. 简单几何(求交点) UVA 11178 Morley's Theorem

    题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /*********************************************** ...

  9. (022)[工具软件]图片浏览 JPEGView

    JPEGView是一款小巧绿色快速的图像浏览工具,并且支持全屏或窗口模式.主页地址: https://sourceforge.net/projects/jpegview/JPEGView软件小巧,但功 ...

  10. 生产环境中使用脚本实现tomcat start|status|stop|restart

    一.在实际生产环境中tomcat启动是在bin目录下采用自带脚本startup.sh启动:使用shutdown.sh关闭.如下图: 再如果对于新手来讲在不知道路径情况下重启是一件头痛的事情(注意没有r ...