九度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

相关文章

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

Go信号处理如何优雅地关闭你的应用

《Go信号处理如何优雅地关闭你的应用》Go中的优雅关闭机制使得在应用程序接收到终止信号时,能够进行平滑的资源清理,通过使用context来管理goroutine的生命周期,结合signal... 目录1. 什么是信号处理?2. 如何优雅地关闭 Go 应用?3. 代码实现3.1 基本的信号捕获和优雅关闭3.2

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

go基础知识归纳总结

无缓冲的 channel 和有缓冲的 channel 的区别? 在 Go 语言中,channel 是用来在 goroutines 之间传递数据的主要机制。它们有两种类型:无缓冲的 channel 和有缓冲的 channel。 无缓冲的 channel 行为:无缓冲的 channel 是一种同步的通信方式,发送和接收必须同时发生。如果一个 goroutine 试图通过无缓冲 channel

log4j2相关配置说明以及${sys:catalina.home}应用

${sys:catalina.home} 等价于 System.getProperty("catalina.home") 就是Tomcat的根目录:  C:\apache-tomcat-7.0.77 <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" /> 2017-08-10

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

【408DS算法题】039进阶-判断图中路径是否存在

Index 题目分析实现总结 题目 对于给定的图G,设计函数实现判断G中是否含有从start结点到stop结点的路径。 分析实现 对于图的路径的存在性判断,有两种做法:(本文的实现均基于邻接矩阵存储方式的图) 1.图的BFS BFS的思路相对比较直观——从起始结点出发进行层次遍历,遍历过程中遇到结点i就表示存在路径start->i,故只需判断每个结点i是否就是stop