更好的阅读体验

Portal

Portal1: Luogu

Portal2: POJ

Description

One cow from each of N farms \((1 \le N \le 1000)\) conveniently numbered \(1 \cdots N\) is going to attend the big cow party to be held at farm #X \((1 \le X \le N)\). A total of \(M (1 \le M \le 100,000)\) unidirectional (one-way roads connects pairs of farms; road \(i\) requires \(T_i (1 \le Ti \le 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 \le X \le N\))的牛的农场举行的派对(\(1 \le N \le 1000\)),农场之间有\(M\)(\(1 \le M \le 100000\))条有向路,每条路长\(Ti\)(\(1 \le Ti \le 100\))。

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

Input

第一行三个整数\(N\),\(M\),\(X\);

第二行到第\(M + 1\)行:每行有三个整数\(A_i\),\(B_i\),\(T_i\) ,表示有一条从\(A_i\)农场到\(B_i\)农场的道路,长度为\(T_i\)。

Output

一个整数,表示最长的最短路得长度。

Sample Input

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

Sample Output

10

Hint

Solution

题目让我们一些奶牛走到一个点,再从那个点走回来的最短路之和的最大值。那么我们直接用dijkstra计算两次最短路(走过去,走回来)就可以了,最后判断一下,那头奶牛需要走的路是最长的,然后问题就解决了。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; const int INF = 0x3f3f3f3f, MAXN = 200005;
struct EDGE {
int nxt, to, val;
} edge[MAXN];
int n, m, S, cnt, U[MAXN], V[MAXN], VAL[MAXN], dis[MAXN], dist[MAXN], head[MAXN];
bool vis[MAXN];
inline void addedge(int u, int v, int val) {//邻接表存图
edge[++cnt].to = v; edge[cnt].val = val; edge[cnt].nxt = head[u]; head[u] = cnt;
}
inline void dijkstra(int S) {//dijkstra最短路
memset(dis, INF, sizeof(dis));
priority_queue< pair<int, int> > Q;
Q.push(make_pair(0, S));
dis[S] = 0;
while (!Q.empty()) {
int u = Q.top().second;
Q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (dis[v] > dis[u] + edge[i].val) {
dis[v] = dis[u] + edge[i].val;
Q.push(make_pair(-dis[v], v));
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &S);
memset(head, -1, sizeof(head));
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &U[i], &V[i], &VAL[i]);
addedge(U[i], V[i], VAL[i]);//正向建图
}
dijkstra(S);
for (int i = 1; i <= n; i++)
dist[i] = dis[i];//记录走到目标点的路程
cnt = 0;
memset(edge, 0, sizeof(edge));
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));//注意清空数组
for (int i = 1; i <= m; i++)
addedge(V[i], U[i], VAL[i]);//反向建图
dijkstra(S);
int Max = -INF;
for (int i = 1; i <= n; i++)
Max = max(Max, dis[i] + dist[i]);//判断那个奶牛是走得最多的
printf("%d\n", Max);
return 0;
}

「Luogu 1821」[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 题解

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

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

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

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

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

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

  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

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

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

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

  9. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

随机推荐

  1. SparkStreaming整合Flume的pull报错解决方案

    先说下版本情况: Spark 2.4.3 Scala 2.11.12 Flume-1.6.0 Flume配置文件: simple-agent.sources = netcat-source simpl ...

  2. 02-15 Logistic回归(鸢尾花分类)

    目录 Logistic回归(鸢尾花分类) 一.导入模块 二.获取数据 三.构建决策边界 四.训练模型 4.1 C参数与权重系数的关系 五.可视化 更新.更全的<机器学习>的更新网站,更有p ...

  3. Spring bean的作用域以及生命周期

    一.request与session的区别 request简介 request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用程序)的一次请求,当请 ...

  4. Jenkins设置BUILD NUMBER初始值

    由于服务器迁移,需要将之前的Jenkins构建项目一并迁移,为了区分构建编号,需要将所有项目的BUILDE NUMBER 初始值定义. 网上有一些资料是更新单个项目的,我们这项目几百个,所以只好自己研 ...

  5. Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行

    本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...

  6. Mac 10.14在新窗口中打开文件夹

    Mac 10.14 Open folders in new window (high Sierra) System Preferences > Dock. Change "Prefer ...

  7. 域渗透基础之Windows 2012创建域控制器

    创建备份域控制器 这里就拿windows 2012 R2来当备份域控 如果一个域内有多个域控制器,可以有如下好处. 提高用户登录的效率:如果同时有多台域控制器对客户提供服务,可以分担审核用户登录身份( ...

  8. Ubuntu16.04 安装apache+mysql+php(LAMP)

    记录下ubuntu环境下安装apache+mysql+php(LAMP)环境. 0x01安装apache sudo apt-get update sudo apt-get install apache ...

  9. Codeforces 986B - Petr and Permutations

    Description\text{Description}Description Given an array a[], swap random 2 number of them for 3n or  ...

  10. opencv::霍夫圆变换

    霍夫圆检测原理 从平面坐标到极坐标转换三个参数 假设平面坐标的任意一个圆上的点,转换到极坐标中: 处有最大值,霍夫变换正是利用这个原理实现圆的检测. cv::HoughCircles 因为霍夫圆检测对 ...