还是最短路径(接前面最短路径)

2024-05-24 19:38
文章标签 路径 最短 前面

本文主要是介绍还是最短路径(接前面最短路径),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*
题目1162:I Wanna Go Home
题目描述:
    The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible. 
    "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
    Would you please tell Mr. M at least how long will it take to reach his sweet home?
输入:
    The input contains multiple test cases.
    The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
    The second line contains one integer M (0<=M<=10000), which is the number of roads.
    The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
    Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i. 
    To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2. 
    Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
输出:
    For each test case, output one integer representing the minimum time to reach home.
    If it is impossible to reach home according to Mr. M's demands, output -1 instead.
样例输入:
2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0
样例输出:
100
90
540
*/
#include <stdio.h>
#include <vector>
using namespace std;
struct E{
int next;
int time;
};
vector<E> edge[601];
bool mark[601];
int dis[601];


int main(){
int n,m,i,j;
while(scanf("%d",&n)!=EOF&&n!=0){
scanf("%d",&m);
for(i=1;i<=n;i++) edge[i].clear();
while(m--){
int a,b,time;
scanf("%d%d%d",&a,&b,&time);
E tmp;
tmp.time =time;
tmp.next =b;
edge[a].push_back(tmp);
tmp.next =a;
edge[b].push_back(tmp);
}
int num[601];
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
for(i=1;i<=n;i++){//改动部分
if(num[i]==2){
for(j=0;j<edge[i].size();j++){
if(num[edge[i][j].next]==1)
edge[i][j].time=0;
}
}
}
    for(i=1;i<=n;i++){
dis[i]=-1;
mark[i]=false;
}
dis[1]=0;
mark[1]=true;
int newp=1;
for(i=1;i<n;i++){
for(j=0;j<edge[newp].size();j++){
int t=edge[newp][j].next;
int time=edge[newp][j].time;
if(mark[t]==true) continue;
if(time==0) continue;//改动部分
if(dis[t]==-1||dis[t]>dis[newp]+time)
dis[t]=dis[newp]+time;
}
int min=11111111;
for(j=1;j<=n;j++){
if(mark[j]==true) continue;
if(dis[j]==-1) continue;
if(dis[j]<min) {
min=dis[j];
newp=j;
}
}
mark[newp]=true;
}
printf("%d\n",dis[2]);
}
return 0;
}

这篇关于还是最短路径(接前面最短路径)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

利用Python把路径转为绝对路径的方法

《利用Python把路径转为绝对路径的方法》在Python中,如果你有一个相对路径并且想将其转换为绝对路径,你可以使用Path对象的resolve()方法,Path是Python标准库pathlib中... 目录1. os.path.abspath 是什么?怎么用?基本用法2. os.path.abspat

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

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