题目链接: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】 题解的更多相关文章

  1. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

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

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

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

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

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

  5. 洛谷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 ...

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

    银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...

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

    更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...

  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. Android RelativeLayout 属性 转自互联网

    // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below 将该控件的底部置于给定ID的控件之下; andro ...

  2. andoid 多线程断点下载

    本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...

  3. 通过TLS回调函数的反调试

    下面是TLS数据结构的定义 typedef struct _IMAGE_TLS_DIRECTORY { DWORD StartAddressOfRawData; DWORD EndAddressOfR ...

  4. inteliJ IDEA使用SVN进行代码管理

    inteliJ 自带版本控制,所以不用像网上其他人说的那样,装第三方插件. 首先装完inteliJ 后,在File-->Setting-->Version Control中选择Subver ...

  5. .net生成cookie,读取cookie,创建特性

    1.登录或注册成功时生成票据和cookie 注释:__JZY_Common_User_Login_Cookie__:就是为了判断当前登录人(例如:管理员和普通用户,可以定义两个常量,可有可无) 2.读 ...

  6. Java 枚举 获取前后元素,下一个元素

    https://blog.csdn.net/jiangshanwe/article/details/79119219

  7. java调用c#dll文件配置

    1 在强大的c#语言和java语言之间,二者难免会因为某些特殊的要求会相互调用. 下面就以java调用c#的dll为例做详细介绍 1  在vs中的环境设置如下图,图片中程序仅作为讲解程序,在项目编译成 ...

  8. JSP初学者4

    Filter可认为是Servlet的“加强版”,他主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理,是个典型的 处理链. 使用Filter完整的流程是:Filte ...

  9. IntelliJ IDEA搭建SpringBoot的小Demo

    首先简单介绍下Spring Boot,来自度娘百科:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进 ...

  10. JSTL核心库

    1 out和set(重点) out <c:out value=”aaa”/> 输出aaa字符串常量 <c:out value=”${aaa}”/> 与${aaa}相同 < ...