博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
阅读量:7020 次
发布时间:2019-06-28

本文共 2637 字,大约阅读时间需要 8 分钟。

P1821 [USACO07FEB]银牛派对Silver Cow Party

题目描述

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

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

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

输入输出格式

输入格式:

 

第一行三个整数N,M, X;

第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

 

输出格式:

 

一个整数,表示最长的最短路得长度。

 

输入输出样例

输入样例#1: 
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
输出样例#1: 
10

说明

 

spfa跑最长路,再求起点到其他点的最短路的时候可以不跑n边spfa,而转为建反向边,然后在跑一遍spfa就好了

这个题跟我们以前做过的一道题很像——洛谷:        

#include
#include
#include
#include
#include
#include
#define N 100000#define maxn 99999999using namespace std;long long ans;int n,m,e,x,y,z,s,tot,dis[N],head[N],dis1[N],dis2[N];int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f;}struct NN{ int x,y,z;}edde[N];struct Edge{ int to,dis,from,next;}edge[N];int add(int x,int y,int z){ tot++; edge[tot].to=y; edge[tot].dis=z; edge[tot].next=head[x]; head[x]=tot;}int spfa(int s){ queue
q; bool vis[N]; int sum=0; for(int i=1;i<=n;i++) dis[i]=maxn,vis[i]=false; q.push(s);vis[s]=true;dis[s]=0; while(!q.empty()) { int x=q.front(); q.pop(); for(int i=head[x];i;i=edge[i].next) { int t=edge[i].to; if(dis[t]>dis[x]+edge[i].dis) { dis[t]=dis[x]+edge[i].dis; if(!vis[t]) { q.push(t); vis[t]=true; } } } vis[x]=false; }}int main(){ n=read(),m=read();e=read(); for(int i=1;i<=m;i++) { x=read(),y=read(),z=read(); add(x,y,z); edde[i].x=x;edde[i].y=y,edde[i].z=z; } spfa(e); for(int i=1;i<=n;i++) dis1[i]=dis[i]; s=tot,tot=0; memset(dis,0,sizeof(dis)); memset(head,0,sizeof(head)); memset(edge,0,sizeof(edge)); for(int i=1;i<=s;i++) add(edde[i].y,edde[i].x,edde[i].z); spfa(e); for(int i=1;i<=n;i++) dis2[i]=dis[i]; for(int i=1;i<=n;i++) if(dis1[i]+dis2[i]>ans) ans=dis1[i]+dis2[i]; printf("%lld",ans); return 0;}

 

转载于:https://www.cnblogs.com/z360/p/8214062.html

你可能感兴趣的文章
通讯实例 modbus_Modbus通讯编程实例
查看>>
亮度翻转_「科技犬」新品游戏本、翻转屏评测汇总:华硕微星荣耀戴尔,选谁...
查看>>
返回的html页面携带参数_这8个HTML元素方法你知道吗
查看>>
四阶魔方25步公式_孙氏四阶魔方公式转换助手使用说明
查看>>
python 判断元素是否在set_Python-BeautifulSoup-如何检查ResultSet是否包含elemen
查看>>
宠物龟 扫地机器人_全球首例!360 扫地机器人竟能识别宠物粑粑,从此告别满地翔!...
查看>>
python getchar_python中的getchar返回permission denied(android 8.0)
查看>>
传染病模型python_传染病动力学模型 SI => SIS => SIR => SEIR(python)
查看>>
html天气预报代码_接口测试平台代码实现114:登录态接口10
查看>>
删除mysql数据库后遗留_mysql数据库被删除后怎么恢复
查看>>
mysql一对多怎么聚合多_mysql多对多
查看>>
mysql 并发控制_mysql并发控制
查看>>
mysql处理高并发的架构_mysql高可用架构设计,处理高并发,大流量!
查看>>
pdo_mysql断线重连_Yii2实现Mysql断线重连
查看>>
mysql 创建视图的好处_mysql视图的作用和优点,视图可以更改么?
查看>>
mysql uuid 和int_MySQL之-uuid做主键与int做主键的性能实测对比详解
查看>>
实验三lr1分析法java_Java中9种常见的CMS GC问题分析与解决(四)
查看>>
hive数据库存入mysql_hive 数据库操作
查看>>
mysql 取30行_sql分页取30-40条记录
查看>>
java递归mysql生成树_java递归生成树结构的数据
查看>>