九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)

2024-04-02 02:18

本文主要是介绍九度OJ 1162:I Wanna Go Home(我想回家) (最短路径),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:870

解决:415

题目描述:

    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
来源:
2011年北京大学计算机研究生机试真题

思路:

题目大意是N个城市分属于两个敌对集团,要从城市1到城市2(分别属于两个集团),只能有一条路跨集团,求最短路径。

我的思路是,求城市1到其集团中其它城市的最短路径,城市2也同样,然后对集团间存在的路径i到j,求d(1,i)+d(i,j)+d(j,2)的最小值。


代码:

#include <stdio.h>#define N 600
#define M 10000
#define INF 1e8int n;
int D[N][N];
int support[N], visit[2][N], dis[2][N];void init()
{for (int i=0; i<n; i++){support[i] = 0;visit[0][i] = visit[1][i] = 0;dis[0][i] = dis[1][i] = INF;for (int j=0; j<n; j++){D[i][j] = INF;}}
}void printdis(int s)
{int i;for (i=0; i<n; i++){if (support[i] == s)printf("%d\n", dis[s][i]);}printf("\n");
}void dijkstra(int s)
{int i, j;for (i=0; i<n; i++){if (support[i] == s)dis[s][i] = D[s][i];}dis[s][s] = 0;visit[s][s] = 1;//printdis(s);int mind;int k;for (i=0; i<n; i++){mind = INF;for (j=0; j<n; j++){if ( support[j] == s && !visit[s][j] && (dis[s][j]<mind) ){mind = dis[s][j];k = j;}}if (mind == INF)break;visit[s][k] = 1;for (j=0; j<n; j++){if ( support[j] == s && !visit[s][j] && (dis[s][k]+D[k][j] < dis[s][j]) ){dis[s][j] = dis[s][k]+D[k][j];}}}//printdis(s);
}int Min(int a, int b)
{return (a<b) ? a : b;
}int main(void)
{int m, i, j;int a, b, d;int min;while (scanf("%d", &n) != EOF && n){init();scanf("%d", &m);for(i=0; i<m; i++){scanf("%d%d%d", &a, &b, &d);D[a-1][b-1] = D[b-1][a-1] = d;}for(i=0; i<n; i++){   scanf("%d", &a);support[i] = a-1;}       dijkstra(0);dijkstra(1);min = INF;for (i=0; i<n; i++){for (j=0; j<n; j++){if (support[i] == 0 && support[j] == 1){min = Min(dis[0][i] + D[i][j] + dis[1][j], min);}}}if (min == INF)printf("-1\n");elseprintf("%d\n", min);}return 0;
}
/**************************************************************Problem: 1162User: liangrx06Language: CResult: AcceptedTime:20 msMemory:2336 kb
****************************************************************/


这篇关于九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/868717

相关文章

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

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

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

Go语言中最便捷的http请求包resty的使用详解

《Go语言中最便捷的http请求包resty的使用详解》go语言虽然自身就有net/http包,但是说实话用起来没那么好用,resty包是go语言中一个非常受欢迎的http请求处理包,下面我们一起来学... 目录安装一、一个简单的get二、带查询参数三、设置请求头、body四、设置表单数据五、处理响应六、超

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:统一使

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

Golang基于内存的键值存储缓存库go-cache

《Golang基于内存的键值存储缓存库go-cache》go-cache是一个内存中的key:valuestore/cache库,适用于单机应用程序,本文主要介绍了Golang基于内存的键值存储缓存库... 目录文档安装方法示例1示例2使用注意点优点缺点go-cache 和 Redis 缓存对比1)功能特性

Go 1.23中Timer无buffer的实现方式详解

《Go1.23中Timer无buffer的实现方式详解》在Go1.23中,Timer的实现通常是通过time包提供的time.Timer类型来实现的,本文主要介绍了Go1.23中Timer无buff... 目录Timer 的基本实现无缓冲区的实现自定义无缓冲 Timer 实现更复杂的 Timer 实现总结在