题目链接: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选手

最新文章

  1. HDU2159 二维完全背包
  2. 聊下git pull --rebase
  3. vlan与交换机端口模式Access,Hybrid,Trunk
  4. sql字符转换函数大全
  5. 复旦大学2013--2014学年第一学期(13级)高等代数I期末考试第七大题解答
  6. Jquery实现文本框输入提示
  7. STL六大组件之——容器知识大扫盲
  8. Java中的NIO和IO的对比分析
  9. linux于test 订购具体解释
  10. ArcGIS JavaScript API本地部署离线开发环境[转]
  11. web前端上传图片的几种方法
  12. 教你快速打造PHP MVC框架
  13. 【学习笔记】非监督学习-k-means
  14. lvs负载均衡(DR模式)
  15. lambda表达式,变量作用域
  16. bzoj1617
  17. Fiddler Web Debugger安装后与浏览器之间的常用设置(辅助爬虫)(图文详解)
  18. 读取Execl表数据 导入数据库
  19. 关于ftpshell脚本中mget中去除多余交互式提示的方法
  20. HDU 1402

热门文章

  1. 使用jxl读取excel内容,并转换成Json,用于Datagrid
  2. SQL脚本整理系列一 表分区
  3. 1个示例 学会 mvc 常用标签
  4. avalon实现分页组件
  5. C#基础知识-数组_ArrayList_List(九)
  6. Ajax简单介绍和使用步骤
  7. Python pip windows安装
  8. 前端路由vue-router介绍
  9. 30 Excellent WordPress Video Tutorials
  10. 用户选择wordpress程序建站需要知道的一些事情 - 安全、优化速度、配置