「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

更好的阅读体验

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;
}

原文地址:https://www.cnblogs.com/shenxiaohuang/p/11594332.html

时间: 2024-08-11 09:55:03

「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party的相关文章

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

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 co

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

[USACO07FEB]银牛派对Silver Cow Party

题目描述 寒假到了,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. 输出格式:

USACO07FEB银牛派对

[题意]n个点m条有向边,求单源最短路最长往返距离 [解题]建两个图 跑两遍spfa 不过有很多细节需要注意 #include<bits/stdc++.h> const int N = 1005; struct node{ int v,w; }; std::vector<node>g[3][N]; std::queue<int>q; int n,k,m,x,y,z,hs,vis[N],f[3][N],ans = -0x3f3f; void spfa(int k){ me

「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓

# 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花费的代价是距离的平方,现在让你求出使得每个点上都有一头奶牛需要花费的最小代价,注意,奶牛只能顺时针移动. # 解题思路 首先断环成链这个大家应该都知道,就是将原序列 copy 一份放到后面去. 然后考虑如果一头奶牛在移动的过程中没有经过其他奶牛,那这一定是最优的方案.还有就是如果一个点有奶牛,那将奶

「Luogu 2367」语文成绩

更好的阅读体验 Portal Portal1: Luogu Description 语文老师总是写错成绩,所以当她修改成绩的时候,总是累得不行.她总是要一遍遍地给某些同学增加分数,又要注意最低分是多少.你能帮帮她吗? Input 第一行有两个整数\(n\),\(p\),代表学生数与增加分数的次数. 第二行有\(n\)个数,\(a_1 \sim a_n\),代表各个学生的初始成绩. 接下来\(p\)行,每行有三个数,\(x\),\(y\),\(z\),代表给第\(x\)个到第\(y\)个学生每人增

「Luogu P2617」Dynamic Rankings

给出一段序列,每次修改某个数的值和询问区间第 k 小.\((1\le n,m\le 10^5,0\le a_i\le10^9)\) Luogu 分析 动态主席树裸题. 树状数组套主席树,树状数组的每个结点相当于一棵主席树,每次修改操作只在对应树状数组的 logn 个结点所对应的主席树上修改,查询时,将 l - 1 和 r 分别对应的 logn 棵主席树作差即可. 时间复杂度: \(O(n\log^2{n})\) 空间复杂度: \(O(n\log{n})\) 代码 #include <bits/s

「Luogu P3302」[SDOI2013]森林

给出一片森林,每个点有一个权值,要求支持动态连边,并回答任意两点间第 k 小权值,强制在线.\((1\le N,M,T \le 8\times 10^4)\) Luogu 分析 求第 k 小权值,这个肯定是用主席树了,但连边该怎么办?LCT?可我不会. 我们可以用启发式合并的方法,连边也就是合并两棵树,我们每次将较小的树连到较大的树上去,更新信息就暴力 dfs 较小树中的每一个点就好了. 代码 #include <bits/stdc++.h> #define N 80003 #define D

「Luogu P3178」[HAOI2015]树上操作

有一棵点数为 \(N\) 的树,以点 \(1\) 为根,且树点有边权.然后有 \(M\) 个操作,分为三种: 操作 1 :把某个节点 \(x\) 的点权增加 \(a\) . 操作 2 :把某个节点 \(x\) 为根的子树中所有点的点权都增加 \(a\) . 操作 3 :询问某个节点 \(x\) 到根的路径中所有点的点权和. Luogu 分析 我们把树上问题利用 \(dfs\) 序转化成序列问题然后直接上线段树解决即可. 考虑将线段树的每个叶子结点设为在原树上的点到根的点权和.对于单点修改,当前结