【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解
题目链接:https://www.luogu.org/problemnew/show/P1821
反向多存一个图,暴力跑两遍
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = ;
const int inf = 0x7fffffff;
int n, m, x, dis1[maxn], dis2[maxn];
bool vis1[maxn] = {}, vis2[maxn] = {};
struct edge{
int len, to, next;
}e1[maxn], e2[maxn];
int head1[maxn], head2[maxn], cnt1, cnt2;
void SPFA1()
{
queue<int> q1;
vis1[x] = ;
dis1[x] = ;
q1.push(x);
while(!q1.empty())
{
int now1 = q1.front();
q1.pop();
vis1[now1] = ;
for(int i = head1[now1]; i!= ; i = e1[i].next)
{
if(dis1[e1[i].to] > dis1[now1]+e1[i].len)
{
dis1[e1[i].to] = dis1[now1]+e1[i].len;
if(!vis1[e1[i].to])
{
vis1[e1[i].to] = ;
q1.push(e1[i].to);
}
}
}
}
}
void SPFA2()
{
queue<int> q2;
vis2[x] = ;
dis2[x] = ;
q2.push(x);
while(!q2.empty())
{
int now2 = q2.front();
q2.pop();
vis2[now2] = ;
for(int i = head2[now2]; i ; i = e2[i].next)
{
if(dis2[e2[i].to] > dis2[now2]+e2[i].len)
{
dis2[e2[i].to] = dis2[now2]+e2[i].len;
if(!vis2[e2[i].to])
{
vis2[e2[i].to] = ;
q2.push(e2[i].to);
}
}
}
}
}
int main()
{
cin>>n>>m>>x;
for(int i = ; i <= n; i++)
{
dis2[i] = inf;
dis1[i] = inf;
}
for(int i = ; i <= m; i++)
{
int u,v,w;
cin>>u>>v>>w;
e1[i].to = v;
e1[i].len = w;
e1[i].next = head1[u];
head1[u] = i; e2[i].to = u;
e2[i].len = w;
e2[i].next = head2[v];
head2[v] = i;
}
SPFA1();
SPFA2();
int ans = ;
for(int i = ; i <= n; i++)
{
ans = max(dis1[i]+dis2[i],ans);
}
cout<<ans;
return ;
}
Ctrl+C Ctrl+V 真毒瘤,弄得我12分反好几次,真是老年OI选手
【luogu P1821 [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 ...
- 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 ...
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 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 ...
- 洛谷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 ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
随机推荐
- angular的基本要点
<body ng-app="Myapp"> <div ng-controller="firstcon"> <h1>hello ...
- sourceTree免登陆
https://www.cnblogs.com/dereckbu/articles/7659674.html
- C++ Memory System Part1: new和delete
在深入探索自定义内存系统之前,我们需要了解一些基础的背景知识,这些知识点是我们接下来自定义内存系统的基础.所以第一部分,让我们来一起深入了解一下C++的new和delete家族,这其中有很多令人吃惊的 ...
- SecureCRT 连接 CentOS虚拟机
SecureCRT 连接 CentOS虚拟机 1.安装SecureCRT SecureCRT是一款支持SSH等协议的终端仿真软件,可以在windows下登录Linux服务器,这样大大方便了开发工作.安 ...
- URAL —— 1255 & HDU 5100——Chessboard ——————【数学规律】
用 k × 1 的矩形覆盖 n × n 的正方形棋盘 用 k × 1 的小矩形覆盖一个 n × n 的正方形棋盘,往往不能实现完全覆盖(比如,有时候 n × n 甚至根本就不是 k 的整倍数). 解题 ...
- hash扩展攻击本地实验
记录一下自己对hash扩展攻击的一些理解,hash扩展攻击主要应用于身份认证,比如对于成功登录的用户可以赋予其一个采用hsah算法加密的cookie值,其中含有未知的密钥. 此后每次在服务器端验证此c ...
- Maven入门之简介与安装
一.Maven简介 1.什么是Maven? Maven是一个项目管理工具和集成编译工具,它主要包含如下内容: –一个项目对象模型(Project Object Model), –一组标准集合, –一个 ...
- 将金额数字转换为大写汉字的js函数
//将金额数字转换为大写汉字的函数 function convertCurrency(money) { //汉字的数字 var cnNums = new Array('零', '壹', '贰', '叁 ...
- Java的输入/输出操作
Java的输入\输出机制 计算机的基本功能就是通过输入输出设备与外部其他设备尽心数据的交互,从其他设备读入数据叫做输入操作,将计算机内的数据写入到其他设备叫做输出操作.可以向计算机发送数据.又可以接受 ...
- AE常用代码(标注要素、AE中画带箭头的线、如何获得投影坐标、参考坐标、投影方式、FeatureCount注意事项)
手上的电脑已经用了将近三年了,想入手一台Surface Pro,所以计划着把电脑上的资料整理下,部分资料打算发到博客上来,资料有同事.也有自己的.也有来自网络的,来源途径太多,也没法详细注明,请见谅! ...