「POJ 3268」Silver Cow Party
Portal
Portal1: POJ
Portal2: Luogu
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;
}
「POJ 3268」Silver Cow Party的更多相关文章
- 【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)
Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...
- POJ 3268:Silver Cow Party 求单点的来回最短路径
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15989 Accepted: 7303 ...
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
- 「POJ Challenge」生日礼物
Tag 堆,贪心,链表 Solution 把连续的符号相同的数缩成一个数,去掉两端的非正数,得到一个正负交替的序列,把该序列中所有数的绝对值扔进堆中,用所有正数的和减去一个最小值,这个最小值的求法与「 ...
- 「POJ 1135」Domino Effect(dfs)
BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...
- 「POJ - 1003」Hangover
BUPT 2017 summer training (16) #2C 题意 n个卡片可以支撑住的长度是1/2+1/3+1/4+..+1/(n+1)个卡片长度.现在给出需要达到总长度,求最小的n. 题解 ...
- 「POJ - 2318」TOYS (叉乘)
BUPT 2017 summer training (16) #2 A 题意 有一个玩具盒,被n个隔板分开成左到u右n+1个区域,然后给每个玩具的坐标,求每个区域有几个玩具. 题解 依次用叉积判断玩具 ...
- 「POJ 2699」The Maximum Number of Strong Kings
题目链接 戳我 \(Describe\) 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边\((u, v)\)或\((v, u)\),表示\(u\)打败\(v\)或 ...
- 「POJ 2182」 Lost Cows
题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...
随机推荐
- 从0开始学FreeRTOS-(创建任务)-2
# 补充 开始今天的内容之前,先补充一下上篇文章[从单片机到操作系统-1](https://jiejietop.gitee.io/freertos-1/)的一点点遗漏的知识点. ```js BaseT ...
- vue3.0的安装使用
关于旧版本 Vue CLI 的包名称由 vue-cli 改成了 @vue/cli. 如果你已经全局安装了旧版本的 vue-cli (1.x 或 2.x),你需要先通过 npm uninstall vu ...
- Windows 10 更新后VMware Workstation pro无法运行 (无需卸载原版本VM)
问题 描述:当前Windows版本是win10-1903,VMware版本比较老旧是VMware Workstation Pro 15.0.4:国庆节后微软推送了一个新的更新补丁,10月10日更新之后 ...
- Cocos2d-x入门之旅[3]动作
Cocos通过动作(Action)可以让精灵动起来,把数个动作组成序列(Sequence)就能让精灵做出连续的动作,在动作中我们可以改变精灵的位置,旋转角度,缩放比例,等等 动作(Action) 首先 ...
- Linux防火墙常用操作
/tcp —— 配置白名单 sudo systemctl start firewalld — 启动防火墙 sudo firewall-cmd --state - 看状态 sudo firewall-c ...
- opencv::积分图计算
利用积分图像,可以计算在某象素的上-右方的或者旋转的矩形区域中进行求和.求均值以及标准方差的计算,并且保证运算的复杂度为O(). #include <opencv2/opencv.hpp> ...
- 概念理解-异步IO
#include <aio.h> /* 函数名 :int aio_write(struct aiocb *aiocbp) 参 数 :struct aiocb *aiocbp 返回值 :执行 ...
- 玩转 RTC时钟库 DS1302
1.前言 最近博主在弄8266编程的时候,偶然发现两个全新时钟模块压仓货: DS1302 DS3231 为了避免资源浪费以及重复编写代码,博主还是抱着尝试的心态去寻找能够同时兼容 DS ...
- LeetCode122——Best Time to Buy and Sell Stock II
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- @RequestParam设置默认可以传空值
设置如下:@RequestParam(value="CbqkJson[]",required=false)String[] CbqkJson required=false 如果不设 ...