[USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队
对于我这种蒟蒻来说,还是不要跑一次单元最短路。跑两次好写呀(~ ̄▽ ̄)~
而题目中是有向图。如果如果按照题意进行最短路的话。就会出现一个单终点最短路和一个单起点最短路
对于单起点自然就是套模板,但对于单终点最短路怎么办呢?
显而易见的是,只有一个终点废话呢你(/゚Д゚)/
这样我们就可以反向存一次有向边。将终点变为起点,这样的话就可以套模板了合着就是刷模板题呀(▼⊿▼)
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int head[1001][2];
struct node
{
int point;
int next;
int dist;
};
node line[101000][2];
int tail;
queue<int>q0;
queue<int>q1;
bool exist[1001][2];
int dis[1001][2];
void add(int x,int y,int val,int d)
{
line[++tail][d].point=y;
line[tail][d].dist=val;
line[tail][d].next=head[x][d];
head[x][d]=tail;
}
int main()
{
int n,m,begin;
scanf("%d%d%d",&n,&m,&begin);
for(int i=1;i<=n;i++)
{
head[i][0]=head[i][1]=-1;
dis[i][0]=dis[i][1]=0x7fffffff;
}
int a,b,c;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c,0);
add(b,a,c,1);
}
int pass;
q0.push(begin);
dis[begin][0]=0;
exist[begin][0]=true;
while(!q0.empty())
{
pass=q0.front();
q0.pop();
exist[pass][0]=false;
int need=head[pass][0];
while(need!=-1)
{
if(dis[line[need][0].point][0]>dis[pass][0]+line[need][0].dist)
{
dis[line[need][0].point][0]=dis[pass][0]+line[need][0].dist;
if(!exist[line[need][0].point][0])
q0.push(line[need][0].point);
}
need=line[need][0].next;
}
}
q1.push(begin);
exist[begin][1]=true;
dis[begin][1]=0;
while(!q1.empty())
{
pass=q1.front();
q1.pop();
exist[pass][1]=false;
int need=head[pass][1];
while(need!=-1)
{
if(dis[line[need][1].point][1]>dis[pass][1]+line[need][1].dist)
{
dis[line[need][1].point][1]=dis[pass][1]+line[need][1].dist;
if(!exist[line[need][1].point][1])
q1.push(line[need][1].point);
}
need=line[need][1].next;
}
}
int ans=-0x7fffff;
for(int i=1;i<=n;i++)
if(i!=begin)
ans=max(ans,dis[i][0]+dis[i][1]);
printf("%d",ans);
}
[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 ...
- 洛谷 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 ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- 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 ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...
- 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解
题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...
随机推荐
- javascript Array数组详解 各种方法
1.数组的声明方法(1): arrayObj = new Array(); //创建一个数组.复制代码 代码如下: var arr1 = new Array(); (2):arrayObj = new ...
- 17082 两个有序数序列中找第k小(优先做) O(logn)
17082 两个有序数序列中找第k小(优先做) 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VC Description 已 ...
- Unity GetComponentsInChildren
1 Component.GetComponentsInChildren 和 GameObject.GetComponentsInChildren 一样吗? API上解释一样. 2. //拿到游戏对象 ...
- linux内核态和用户态小结
一 内核态和用户态的区别 当进程执行系统调用而陷入内核代码中执行时,我们就称进程处于内核状态.此时处理器处于特权级最高的(0级)内核代码.当进程处于内核态时,执行的内核代码会使用当前的内核栈.每个进程 ...
- linux 下库的深入调研
linux操作系统中,linux库文件路径还是比较常用的,于是我研究了一下linux库文件路径,在这里拿出来和大家分享一下,希望对大家有用. 库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程 ...
- 1.C#中的注释符
1.软件行业的道德规范 (1).程序员在日常写代码的过程中,一定要养成注释的好习惯,方便后面对理解和使用. (2).在给标识符命名的时候一定要规范,有理有据的,名字不能瞎写. 2.注释 注释符的作用: ...
- div模拟textarea自适应高度
之前在公司做项目的时候,有这么一个需求,要我写一个评论框,可以随着评论的行数增加而自动扩大,最开始我想用textarea实现,但是后来尝试后发现textarea并不适合,textarea的高度不会随着 ...
- Redis入门--(二)Redis的概述
1.Redis的由来 创始人觉得Mysql不好用,就自己写了: 国内使用Redis的网站有新浪微博,知乎: 国外GitHub: VMWare也支持redis的开发 2.Redis的概述 官方提供的测试 ...
- CSS之background-image:在一个元素中设置给定数量的背景图片
众所周知,可以通过设置background-repeat的值来改变背景图片的重复次数.但有一个问题,background-repeat的值不是让图片只有1个,就是让图片铺满.如果只想设置给定数量的图片 ...
- Design Pattern ->Prototype
Layering & Contract Philosophy With additional indirection Prototype The example code is as foll ...