【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解
2024-09-01 08:43:50
题目链接: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选手
最新文章
- HDU2159 二维完全背包
- 聊下git pull --rebase
- vlan与交换机端口模式Access,Hybrid,Trunk
- sql字符转换函数大全
- 复旦大学2013--2014学年第一学期(13级)高等代数I期末考试第七大题解答
- Jquery实现文本框输入提示
- STL六大组件之——容器知识大扫盲
- Java中的NIO和IO的对比分析
- linux于test 订购具体解释
- ArcGIS JavaScript API本地部署离线开发环境[转]
- web前端上传图片的几种方法
- 教你快速打造PHP MVC框架
- 【学习笔记】非监督学习-k-means
- lvs负载均衡(DR模式)
- lambda表达式,变量作用域
- bzoj1617
- Fiddler Web Debugger安装后与浏览器之间的常用设置(辅助爬虫)(图文详解)
- 读取Execl表数据 导入数据库
- 关于ftpshell脚本中mget中去除多余交互式提示的方法
- HDU 1402