POJ - 3268 Silver Cow Party

【代码超详解】POJ 3268 Silver Cow Party(Dijkstra 算法 + 思维) 一、题目描述 二、算法分析说明与代码编写指导 每头牛都会到点 x 参加聚会,然后返回。但路是单向的,因此回去的路可能与来的路不同。 如果对每个点都跑一次 Dijkstra,或者直接跑一次 Floyd 算法,由于复杂度达到 O(n^3),在本题的数据规模下会超时。 从 x 回去的最短路可以直接套 Dijkstra 算法求解单源最短路径。从各点来到 x 的最短路径也只需要跑一次 Dijkstra 就... 阅读详情

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20881 Accepted: 9550

Description

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?

Input

Line 1: Three space-separated integers, respectively:  N,  M, and  X 
Lines 2.. M+1: Line  i+1 describes road  i with three space-separated integers:  Ai,  Bi, and  Ti. The described road runs from farm  Ai to farm  Bi, requiring  Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

Source

把边正向建一个图,在反向建一个图,两遍dj,正向建是求回去的最短路径,而反向建是因为用dj求每个点到单点的最短距离复杂度很高,反向建图后求终点到每个点的最短路径就可以了

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define eps 1e-9
#define PI 3.141592653589793
#define bs 1000000007
#define bsize 256
#define MEM(a) memset(a,0,sizeof(a))
#define inf 0x3f3f3f3f
#define rep(i,be,n) for(i=be;i<n;i++)
typedef long long ll;
using namespace std;
int mp[2][1005][1005];
int book[1005];
int x,n,m;
int dijis(int k)
{
	int index,i,j,minn;
	MEM(book);
	book[x]=1;
	for(i=1;i<=n;i++)
	{	
		minn=inf;
		for(j=1;j<=n;j++)
		{
			if(!book[j]&&mp[k][x][j]<minn)
			{
				index=j;
				minn=mp[k][x][j];
			}
		}
		book[index]=1;
		for(j=1;j<=n;j++)
		{
			if(!book[j]&&mp[k][index][j]+minn<mp[k][x][j])
			{
				mp[k][x][j]=mp[k][index][j]+minn;
			}
		}
	}
}
int main()
{
	int i,j,a,b,t;
	memset(mp,inf,sizeof(mp));
	scanf("%d %d %d",&n,&m,&x);
	while(m--)
	{
		scanf("%d %d %d",&a,&b,&t);
		if(mp[0][a][b]>t)
		{
			mp[0][a][b]=t;
		}
		if(mp[1][b][a]>t)
		{
			mp[1][b][a]=t;
		}
	}
	dijis(0);
	dijis(1);
	int ans=0;
	for(i=1;i<=n;i++)
	{
		if(i==x)
		continue;
		ans=max(ans,mp[0][x][i]+mp[1][x][i]);
	}
	printf("%d\n",ans);
	return 0;
 }



Mplus实战:如何用潜在类别分析(LCA)快速搞定心理学数据分类? 本文详细介绍了如何运用Mplus软件进行潜在类别分析(LCA),以快速处理心理学数据并识别内部异质性群体。文章从数据准备、Mplus语法编写、模型拟合与选择,到结果解读与高级应用(如纳入协变量),提供了完整的实战指南,帮助研究者从变量视角转向个体视角,有效挖掘数据中的潜在子类别。 阅读详情

相关推荐

滚动轴承振动信号特征解析与故障诊断模型构建

本文深入解析了滚动轴承振动信号的三大来源与特征,并详细阐述了从时域、频域到时频域的特征提取方法。通过构建基于机器学习(如随机森林、SVM)或深度学习的故障诊断模型,结合实战代码示例,为设备维护工程师提供了一套从信号分析到智能诊断的完整解决方案,旨在实现精准的故障识别与预测性维护。

qsc901234567的博客 368

POJ 3268 Silver Cow Party

POJ 3268 Silver Cow Party Description 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 ...

旺崽的博客 975

ChatGPT设置密钥后Codex免手机验证:配置顺序与身份凭证复用机制

API Key 是一种用于身份识别的凭证,其核心原理是通过预配置的密钥替代交互式登录流程。在开发工具中,密钥认证优先级通常高于会话认证,当本地存在有效密钥时,工具会直接复用该凭证,从而减少重复验证环节。这种机制的技术价值在于提升开发效率与配置一致性,尤其适用于多设备切换、CI/CD 流水线及编程辅助工具等场景。以 Codex 为例,在 ChatGPT 界面提前配置 API 密钥后,Codex 启动时可读取持久化存储的密钥,避免触发手机号验证,实现身份凭证的跨入口复用。

weixin_33940469的博客 472

POJ 3268 Silver Cow Party 【反向dijkstra】

POJ 3268 Silver Cow Party 题目大意: 每个农场有一只奶牛,所有奶牛都要去指定的一个农场X中聚会,聚会结束后各自回自己的农场, 求所有的奶牛出门参加聚会再回到家中的最短用时,即求出耗时最长的奶牛的时间 具体思路: 最短路求解,先求x到各个农场的时间,再将各边反向,再求一次x到各个农场的时间。 将两个时间相加,最小的时间即为所求 (求图中所有点到指定点的最短距离可以转化为求指...

指弹键盘哼民谣 810

Abaqus 单元与全局刚度矩阵导出实战指南:从INP修改到结果解析

本文提供了Abaqus中导出单元刚度矩阵和全局刚度矩阵的完整实战指南。通过详细步骤讲解INP文件修改、关键指令插入及结果文件解析,帮助工程师、研究人员和二次开发者深入理解有限元软件内核,实现理论验证、二次开发与深度问题调试。

k8s6orchestrator的博客 898

ADS 2024 Circuit Envelope 仿真:3步配置调制源,分析5G NR EVM与ACPR

本文详细介绍了ADS 2024 Circuit Envelope仿真技术在5G NR调制源配置与关键指标分析中的应用。通过3步配置调制源,工程师可以高效评估EVM与ACPR等关键指标,特别适用于功放设计中的非线性效应诊断。文章还提供了优化仿真设置和精确提取EVM、ACPR的实用方法,助力5G射频前端设计。

weixin_28559803的博客 383

POJ 3268 Silver Cow Party

POJ 3268 Silver Cow Party -007 Description 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 (...

GG_Bond的博客 173

华为Atlas200DK实战指南:从零搭建AI开发环境到人像语义分割模型部署

本文提供了一份详尽的华为Atlas 200DK AI开发环境搭建与模型部署实战指南。文章采用合设环境方案,从SD卡准备、系统镜像烧录、网络配置到必备工具安装,逐步引导开发者完成环境搭建。核心部分详细讲解了如何利用华为ATC工具将TensorFlow模型转换为昇腾专用格式,并最终成功编译和运行人像语义分割模型,实现实时人像识别效果。

weixin_29309213的博客 356

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow PartyDescription 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

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 1618

AMBA-CHI协议详解(三)

Write Transactions、Atomic transactions、 Stash transactions、 Dataless transactions的等各个事务的事务流。

Crazzy_M的博客 3180

多agv系统调度的MATLAB仿真

从起点出发,到货物点领取货物最后到卸货点,以完成任务总时间最短为目标进行单目标优化,约束条件为,任务不冲突,路径不冲突,要能实现路径规避 ,4-6台agv一起工作。左右货架间隔可通过1个agv,上下货架间隔可通过两个agv,通道为双向行驶通道。一台agv只能带一个货物,优化目标为完成任务总的时间最短,agv的速度是匀速,转弯也不会减速,取货物时间为固定值。上面的图里面货物看为正方形跟agv一样的长度大小,agv试做是跟货物一样大小的正方形agv的速度是每秒1个车身。多台agv一起完成...

FPGA/MATLAB学习教程/源码/项目合作开发 7563

[POJ 3268 Silver Cow Party ]Dijkstra

[POJ 3268 Silver Cow Party ]Dijkstra知识点:Dijkstra1. 题目链接[POJ 3268 Silver Cow Party ]2. 题意描述有nn个顶点,mm条边的有向图。求图中所有,从该节点出发到点xx,然后从xx回到该节点的最短路最长有多长。 (1≤N≤1000,1≤M≤100,000)(1 ≤ N ≤ 1000,1 ≤ M ≤ 100,000)3. 解

Fighting! 539

buildozer

/home/chenyang/new_kivy/.buildozer/android/platform/build/build/other_builds/hostpython3/desktop/hostpython3/Lib

东方佑 16万+

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description 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 tota...

261

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

[POJ3268](最短路dijkstra+反向存图)Silver Cow Party

POJ3268Silver Cow Party 题目 ne 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) u...

Z7784562的博客 326

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

POJ 3268 Silver Cow Party 最短路

Description 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 require

yulong_D的博客 234

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

POJ 3268 Silver Cow Party(双向最短路)

Silver Cow Party Description 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 ≤

nudt_oys的博客 628

Silver Cow Party POJ - 3268

Silver Cow Party POJ - 3268 给出n,m,代表牛数和边数,接着是m条有向边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出 N<=1000,M<=100,000 Input Line 1: Three spac...

ln2037的博客 220

poj3268Silver Cow Party——最短路变形

poj3268Silver Cow Party:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16780   Accepted: 7663 Descriptio

hahaAll的博客 411

POJ.3268 Silver Cow Party (Dijkstra)

POJ.3268 Silver Cow Party (Dijkstra)题意分析稍后补代码总览#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <cmath> #define nmax 1005 #define i

Pengwill's Blog 384

Dijkstra算法:POJ No 3268 Silver Cow Party

Dijkstra算法:POJ No 3268 Silver Cow Party 题目:http://poj.org/problem?id=3268 题解:使用 priority_queue队列对dijkstra算法进行优化 #include <iostream> #include <algorithm> #inc...

豆子 209

POJ 3268 Silver Cow Party(Dijkstra)

POJ 3268 Silver Cow Party(Dijkstra) http://poj.org/problem?id=3268 题意:有N头牛要去参加牛X那里的聚会,现在除了X牛外,其他N-1头牛都要走到X牛那里去.给你M条有向边,现在问你任意一头牛从自己的位置走到X牛那,然后再走回来(来回都选择最短路径走)的话,需要的总时间的最大值是多少?即从所有N-1头牛中找那个最大的来回时间.

code 1042

POJ--3268 Silver Cow Party(最短路)

题目电波:POJ--3268 Silver Cow Party 跑两遍 dijkstra 就好了 弱智题 #include<iostream> #include<stdio.h> #include<algorithm> #include<queue> #include<cstring> using names...

weixin_30786617的博客 82

POJ-3268 Silver Cow Party(最短路spfa算法)

POJ-3268 Silver Cow PartySilver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19989 Accepted: 9128DescriptionOne cow from each of N farms (1 ≤ N ≤ 1000) convenientl

废宅工程狮养成日志 420

POJ 3268 Silver Cow Party Dijkstra 单源最短路

POJ 3268 Silver Cow Party题目描述:  题目描述:POJ 3268 Silver Cow Party题目大意:  有NN个农场,在这些农场里有mm条单向的路。每个农场出一个奶牛,在XX农场聚会。问每个奶牛至少要花多久在往返的路上。解题思路:  题目说至少花多少时间在来回的路上,也就是说在求再来回路上用时最短的母牛的所用时间。显然可以将这个划分为两部分。 每只牛从X

Running Boy的AC Dream 442

POJ3268 Silver Cow Party 反向建图(Dijkstra)

Sample Input 4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3 Sample Output 10 注意: 在写正向和反向dij的时候注意替换全部的dis和visited为另外一个数组 否则你就会debug两小时(逃) 考虑提前终止条件,当前点的更新最好放在循环外边 考虑用邻接数组代替邻接链表?(节省时间) 代码如下: #include <stdio.h> #include <iostream> #inclu.

Lewis的博客 237
上一篇: poj2019 Cornfields
下一篇: POJ - 2182 D - Lost Cows 暴力//树状数组加二分
jerans
博客等级 码龄10年 54粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值