poj 3268 Silver Cow Party(单源最短路径Dijkstra·最小环)

2024-03-27 23:32

本文主要是介绍poj 3268 Silver Cow Party(单源最短路径Dijkstra·最小环),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11757

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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:  NM, and  X
Lines 2..  M+1: Line  i+1 describes road  i with three space-separated integers:  AiBi, 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.
终点给出,是一个定点。所有的牛犊都会到达该点并且由通过不同的道路回到原点。由此想到单源最短路径,把终点看作源点S,但是怎么回去呢?看了一个大神的博客,矩阵转置(第一次自己转置都写错了||-_-)。效果:(1,2) (2,7) (7,4)  --> (2,1) (7,2) (4,7) means (4,7) (7,2) (2,1)  起点和终点的地位互换!这样再一次用Dijkstra或者spfa等算法求出回家的最短路径就构成了环路。
关于矩阵转置举个例子:
for(int i=0;i<4;i++){
         for(int j=0;j<=i;j++){
             swap(a.m[i][j],a.m[j][i]);
         }
}
不是:
for(int i=0;i<4;i++){
         for(int j=0;j<4;j++){
             swap(a.m[i][j],a.m[j][i]);
         }
}
来看看效果:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct matrie{int m[4][4];
};
matrie a={
1,2,3,4,
7,3,5,5,
6,6,6,6,
0,0,0,0
};
void show(){cout<<"show: \n";for(int i=0;i<4;i++){for(int j=0;j<4;j++){cout<<a.m[i][j]<<" ";}cout<<endl;}
}
void trans(){for(int i=0;i<4;i++){for(int j=0;j<=i;j++){swap(a.m[i][j],a.m[j][i]);cout<<i<<" "<<j<<": "<<endl;show();}}
}int main()
{trans();return 0;
}
0 0: 
show: 
1 2 3 4 
7 3 5 5 
6 6 6 6 
0 0 0 0 
1 0: 
show: 
1 7 3 4 
2 3 5 5 
6 6 6 6 
0 0 0 0 
1 1: 
show: 
1 7 3 4 
2 3 5 5 
6 6 6 6 
0 0 0 0 
2 0: 
show: 
1 7 6 4 
2 3 5 5 
3 6 6 6 
0 0 0 0 
2 1: 
show: 
1 7 6 4 
2 3 6 5 
3 5 6 6 
0 0 0 0 
2 2: 
show: 
1 7 6 4 
2 3 6 5 
3 5 6 6 
0 0 0 0 
3 0: 
show: 
1 7 6 0 
2 3 6 5 
3 5 6 6 
4 0 0 0 
3 1: 
show: 
1 7 6 0 
2 3 6 0 
3 5 6 6 
4 5 0 0 
3 2: 
show: 
1 7 6 0 
2 3 6 0 
3 5 6 0 
4 5 6 0 
3 3: 
show: 
1 7 6 0 
2 3 6 0 
3 5 6 0 
4 5 6 0 
是的,就是这样。
Dijkstra:基于贪心的思想。不足之处在于不能有负边,用于求解单源最短路径问题。这里没有负边问题,所以我就用它吧。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1005,INF=0x3f3f3f3f;
int dis1[maxn],dis2[maxn],map[maxn][maxn],pre[maxn];
int n,m,s;
bool p[maxn];
void Dijkstra(int dis[]){memset(p,0,sizeof(p));for(int i=1;i<=n;i++){if(i!=s){ dis[i]=map[s][i]; pre[i]=s; }else pre[i]=0;}dis[s]=0;p[s]=1;for(int i=1;i<n;i++){  //循环n-1次,求s到其他n-1个点的最短距离int minm=INF,k=0;for(int j=1;j<=n;j++){ //寻找最近的点if(p[j]==0&&dis[j]<minm){minm=dis[j];k=j;}}if(k==0) return;  //没有可以拓展的点p[k]=1;for(int j=1;j<=n;j++){  if(p[j]==0&&map[k][j]!=INF&&dis[j]>dis[k]+map[k][j]){ //利用最近的点更新其他点的距离dis[j]=dis[k]+map[k][j];pre[j]=k;}}}
}
void trans(){for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){swap(map[i][j],map[j][i]);}}
}
int main()
{//freopen("cin.txt","r",stdin);while(cin>>n>>m>>s){int a,b,c;memset(map,0x3f,sizeof(map));for(int i=1;i<=n;i++) map[i][i]=0;while(m--){scanf("%d%d%d",&a,&b,&c);if(a!=b&&map[a][b]>c)map[a][b]=c;}Dijkstra(dis1);trans();Dijkstra(dis2);int ans=0;for(int i=1;i<=n;i++){if(i!=s)ans=max(ans,dis1[i]+dis2[i]);}printf("%d\n",ans);}return 0;
}


这篇关于poj 3268 Silver Cow Party(单源最短路径Dijkstra·最小环)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853768

相关文章

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使