[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 < ...
随机推荐
- 转 Django中的Form
https://www.cnblogs.com/chenchao1990/p/5284237.html Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 ...
- public class 与 class 的区别
public class 与 class 的区别 1.一个类前面的public是可有可无的 2.如果一个类使用 public 修饰,则文件名必须与类名一致 3.如果一个类前面没有使用public修饰, ...
- Unity GetComponentsInChildren
1 Component.GetComponentsInChildren 和 GameObject.GetComponentsInChildren 一样吗? API上解释一样. 2. //拿到游戏对象 ...
- [转]jQuery.getJSON的缓存问题的解决办法
本文转自:http://mfan.iteye.com/blog/974132 今天做测试工作,发现了一个令我费解的问题,jquery的getJson方法在firefox上运行可以得到返回的结果,但是在 ...
- Murano Weekly Meeting 2015.09.01
Meeting time: 2015.September.1st 1:00~2:00 Chairperson: Nikolay Starodubtsev, from Mirantis Meeting ...
- a :hover 和a:hover 区别
article a :hover { color: red;} 上面表示 article 内所有a 标签的所有子标签在hover时是红色 article a:hover { color: red; ...
- Nginx主主负载均衡架构
在和一些朋友交流Nginx+Keepalived技术时,我虽然已成功多次实Nginx+Keepaived项目方案,但这些都是用的单主Nginx在工作,从Nginx长期只是处于备份状态,所以我们想将二台 ...
- Linux Shell 录制并回放终端会话
当别人演示某些命令时,一时可能记不住,解决办法之一是把演示时的过程录制下来,视频当然最完美但是成本高.利用script和scriptrelay命令可以录制命令的次序和时序. 录制 script -t ...
- nodejs操作文件
var fs = require('fs'); var txt = "以上程序使用fs.readFileSync从源路径读取文件内容,并使用fs.writeFileSync将文件内容写入目标 ...
- bundle绑定资源表
1.注册绑定资源表 在application_Start函数中: (注意不要加拓展名,否则压缩时出问题) BundleTable.Bundles.Add(new ScriptBundle(" ...