题目简叙:

寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

分析:

其实这道题的考点就是单元最短路径和单终点最短路径。

单终点最短路径其实就可以把所有的边反过来,直接就转换为单源最短路径了。

于是此题的核心就是跑两遍dijkstra或spfa了(本人偏好dijkstra)。

还有就是注意变量不要重复

代码:

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
struct edge
{
int to,val;
};
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q,w;
vector<edge> e[1005],f[1005];
int n,m,s;
int dis[1005],his[1005],vis[1005],visit[1005];
void dijkstra_zheng()//正常的单元最短路,也就是牛们散伙时的最短路
{
for(int i=1;i<=n;i++)
{
dis[i]=2147483647;
}
dis[s]=0;
q.push(make_pair(0,s));
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(vis[x]==1)
continue;
vis[x]=1;
for(int i=0;i<e[x].size();i++)
{
int y=e[x][i].to;
if(dis[x]+e[x][i].val<dis[y])
{
dis[y]=dis[x]+e[x][i].val;
q.push(make_pair(dis[y], y));
}
}
}
}
void dijkstra_dao()//反过来的最短路,也就是牛们去开派对的最短路
{
for(int i=1;i<=n;i++)
{
his[i]=2147483647;
}
his[s]=0;
w.push(make_pair(0,s));
while(!w.empty())
{
int x=w.top().second;
w.pop();
if(visit[x]==1)
continue;
visit[x]=1;
for(int i=0;i<f[x].size();i++)
{
int y=f[x][i].to;
if(his[x]+f[x][i].val<his[y])
{
his[y]=his[x]+f[x][i].val;
w.push(make_pair(his[y], y));
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
edge tmp;
tmp.to=y;
tmp.val=z;
e[x].push_back(tmp);
tmp.to=x;
tmp.val=z;
f[y].push_back(tmp);//倒着再存一遍
}
dijkstra_zheng();
dijkstra_dao();
int ans=0;
for(int i=1;i<=n;i++)
{
if(his[i]+dis[i]>ans)
{
ans=his[i]+dis[i];
}//求最大值就行了
}
printf("%d",ans);
}

码风较丑陋,请多包涵

[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. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

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

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

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

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

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

  7. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

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

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

  9. 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...

随机推荐

  1. 用python的curl和lxml来抓取和分析网页内容

    Curl是一个强大的URL语法的客户端,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, PO ...

  2. 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))

    简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...

  3. firemonkey 手机屏幕自适应程序问题

    我是新手.在我才学了2个星期的时候,那个白痴老板说什么手机屏幕自适应程序,我当时不能理解呀,觉得用Delphi的布局设计不就行了吗.结果他说:我就是想让控件内容什么的放在小屏幕手机上也不出来.我就说, ...

  4. 比较DirectX和OpenGL的区别(比较详细)

    OpenGL是个专业的3D程序接口,是一个功能强大,调用方便的底层3D图形库.OpenGL的前身是SGI公司为其图形工作站开发的IRIS GL.IRIS GL是一个工业标准的3D图形软件接口,功能虽然 ...

  5. Dependency Injection 筆記 (1)

    <.NET 依賴注入>連載 (1) 本文从一个基本的问题开始,点出软件需求变动的常态,以说明为什么我们需要学习「依赖注入」(dependency injection:简称 DI)来改善设计 ...

  6. Spring的Bean的生命周期

    一:生命周期执行的过程如下:1) spring对bean进行实例化,默认bean是单例.2) spring对bean进行依赖注入.3) 如果bean实现了BeanNameAware接口,spring将 ...

  7. surging 微服务引擎 2.0 会有多少惊喜?

    surging 微服务引擎从2017年6月至今已经有两年的时间,这两年时间有多家公司使用surging 服务引擎,并且有公司搭建了CI/CD,并且使用了k8s 集群,这里我可以说下几家公司的服务搭建情 ...

  8. 微信商城小程序 带java后台源码

    微信小程序商城(Java版) 技术选型 1 后端使用技术 1.1 spring-web-4.0.2.RELEASE 1.2 mybatis3.2.8 1.3 shiro1.2.3 1.4 servle ...

  9. spring 5.x 系列第8篇 —— 整合Redis客户端 Jedis和Redisson (代码配置方式)

    文章目录 一.说明 1.1 Redis 客户端说明 1.2 Redis可视化软件 1.3 项目结构说明 1.3 依赖说明 二.spring 整合 jedis 2.1 新建基本配置文件和其映射类 2.2 ...

  10. spring cloud 系列第7篇 —— sleuth+zipkin 服务链路追踪 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.简介 在微服务架构中,几乎每一个前端的请求都会经过多个服务单元协调来提 ...