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

相关文章

随想录 Day 69 并查集 107. 寻找存在的路径

随想录 Day 69 并查集 107. 寻找存在的路径 理论基础 int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好vector<int> father = vector<int> (n, 0); // C++里的一种数组结构// 并查集初始化void init() {for (int i = 0; i < n; ++i) {father[i] = i;}

SQL Server中,用Restore DataBase把数据库还原到指定的路径

restore database 数据库名 from disk='备份文件路径' with move '数据库文件名' to '数据库文件放置路径', move '日志文件名' to '日志文件存放置路径' Go 如: restore database EaseWe from disk='H:\EaseWe.bak' with move 'Ease

LeetCode--214 最短回文串

题目 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 示例 示例 1:输入: "aacecaaa"输出: "aaacecaaa"示例 2:输入: "abcd"输出: "dcbabcd" 思路: 我们需要添加多少个字符与给定字符串的前缀子串回文的长度有关. 也就是说去掉其前缀的回文子串,我们只需要补充剩下的子串的逆序

C# 命名管道中客户端访问服务器时,出现“对路径的访问被拒绝”

先还原一下我出现错误的情景:我用C#控制台写了一个命名管道服务器,然后用ASP.NET写了一个客户端访问服务器,运行之后出现了下面的错误: 原因:服务器端的访问权限不够,所以是服务器端的问题,需要增加访问权限。(网上很多都说是文件夹的权限不够,情况不同,不适用于我这种情况) 解决办法: (1)在服务器端相应地方添加以下代码。 PipeSecurity pse = new PipeSec

代码随想录算法训练营第三十九天|62.不同路径 63. 不同路径 II 343.整数拆分 96.不同的二叉搜索树

LeetCode 62.不同路径 题目链接:62.不同路径 踩坑:二维的vector数组需要初始化,否则会报错访问空指针 思路: 确定动态数组的含义:dp[i][j]:到达(i,j)有多少条路经递推公式:dp[i][j] = dp[i-1][j] + dp[i][j-1]初始化动态数组:dp[0][0] = 1遍历顺序:从左到右,从上到下 代码: class Solution {pu

Offending ECDSA key in /home/lierjun/.ssh/known_hosts:1

问题描述: 使用终端进行远程连接linux 连接格式:ssh root@ip 结果发出警告信息,信息提示: Offending ECDSA key in /home/user/.ssh/known_hosts:1 解决办法: cd /home/user/.ssh cat known_hosts sed -i '1d' known_hosts 然后再次进行链接可以了

Go 三色标记法:一种高效的垃圾回收策略

💝💝💝欢迎莅临我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:「stormsha的主页」👈,持续学习,不断总结,共同进步,为了踏实,做好当下事儿~ 专栏导航 Python系列: Python面试题合集,剑指大厂Git系列: Git操作技巧GO系列: 记录博主学习GO语言的笔

Go语言中的go.mod与go.sum

问题1:什么是go.mod以及它是用来解决什么问题的? go mod 是 Go 语言引入的包管理工具,用于解决 Go 语言项目在依赖管理方面的问题。 传统上,若不使用go mod,则需要要通过GOPATH来管理依赖,而这种方式存在一些问题: 如1. 包管理依赖不明确 2. 依赖库的版本管理 3. 需要手动管理同步依赖的复杂性等 而go mod可以帮助开发者在项目中明确管理依赖的版

client-go删除job同时删除job关联的pod

问题描述 client-go使用以下方式删除job时,并不会把其关联的pod删除,从而导致这些pod成为了孤儿(orphan): err := clientSet.BatchV1().Jobs(namespace).Delete(name, &metav1.DeleteOptions{}) 在删除job的时候将job关联的pod也删除的方法: propagationPolicy := m