I Wanna Go Home

牛客_BFC_I_Wanna_Go_Home_C++ 牛客_BFC_I_Wanna_Go_Home_C++_by_一把健 题目描述 ​ 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 ... 阅读详情
总时间限制:
1000ms
内存限制:
65536kB
描述

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 integersA, B and T, which means the road between city A and cityB 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 cityi.

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



题解:就是找到回家的最短路,从城市1->城市2,但是有个条件,就是每个点有值1或2,为了安全,值只能变一次,也就是说从支持1的城市到支持2的城市后,不能再返回支持1的城市,也就是说从值为1->值为2这是个单向的,那么只要在基本赋值完成后,弄成单向图就行啦,我把从值为2的城市到值为1的城市的边置为INF,就不能走这条路,剩余的就是单源最短路径Dijksra了

#include<bits/stdc++.h>
using namespace std;

const int INF = 0x9f9f9f;
int n,m;
int maze[700][700];
int sup[10010];
int vis[700];
int dis[700];

void dijksra(int start)
{
	memset(vis,0,sizeof(vis));
	memset(dis,0,sizeof(dis));
	dis[1] = 0;
	for(int i = 2; i <= n; i++)
		dis[i] = maze[1][i];
	vis[start] = 1;	
	for(int i = 1; i <= n; i++){
		int tmp = INF;
		int k;
		
		for(int j = 1; j <= n; j++){
			if (vis[j] == 0 && dis[j] < tmp){
				tmp = dis[j];
				k = j;
			} 
		}
		
		vis[k] = 1;
		for(int j = 1; j <= n; j++){
			if (vis[j] == 0 && dis[j] > dis[k]+maze[k][j])
				dis[j] = dis[k]+maze[k][j];
		}
	}
}

int main()
{
	while(~scanf("%d",&n)){
		if (n == 0)
			break;
		
		for(int i = 0; i <= n; i++)
			for(int j = 0; j <= n; j++)
				if (i == j)
					maze[i][j] = 0;
				else	maze[i][j] = INF;
				
		memset(sup,0,sizeof(sup));
		cin >> m;
		for(int i = 0; i < m; i++){
			int s,e,v;
			cin >> s >> e >> v;
			maze[s][e] = maze[e][s] = v;
		}
		for(int i = 1; i <= n; i++)
			cin >> sup[i];
		
		for(int i = 1; i <= m; i++){
			for(int j = 1; j <= m; j++){
				if (i == j)
					continue;
				else{
					if (sup[i] == 1 && sup[j] == 2)
						maze[j][i] = INF;
					else if (sup[i] == 2 && sup[j] == 1)
						maze[i][j] = INF; 
				}
			}
		}
		dijksra(1);
		if (dis[2] == INF)
			printf("-1\n");
		else
			printf("%d\n",dis[2]);
	}	

	return 0;
}

小米15修复“NV损坏” 去除主板校验电阻步骤解析与图示 小米15系列将搭载骁龙8至尊版处理器,采用台积电3nm工艺。文章分析了手机出现"NV损坏"故障的表现及修复方法,包括通信异常、系统启动问题等典型症状,并介绍了去除主板校验电阻和刷写工程固件两种修复方案。重点讲解了去除主板电阻后刷写QCN文件的操作流程,强调在维修过程中需要结合分析与思考。适用于小米15系列特定机型(24129PN74G/I/C)的硬件修复。 阅读详情

相关推荐

【自然语言处理(NLP)实战】LSTM网络实现中文文本情感分析(手把手与教学超详细)

在当今数字化时代,人们在社交媒体、评论平台以及各类在线交流中产生了海量的文本数据。这些数据蕴含着丰富的情感信息,从而成为了深入理解用户态度、市场趋势,甚至社会情绪的宝贵资源。自然语言处理(NLP)的发展为我们提供了强大的工具,使得对文本情感进行分析成为可能。在这个领域中,长短时记忆网络(LSTM)凭借其能够捕捉文本序列中长距离依赖关系的能力,成为了情感分析任务中的一项重要技术。通过详细的步骤和示例代码,深入了解如何处理中文文本数据、构建LSTM模型、进行训练和评估。

halo的博客 2万+

I wanna go home

题目网址:I wanna go home 题目大意:给出n个城市 ,给出m对城市之间的距离,再给出每一个城市的所属的阵营,要求求出从城市1到城市2的最小的路径长度,城市1总是属于阵营1的,城市2是属于阵营2的。这条路径上最多只有一次从不同阵营穿过的路径,其实

coder 673

合成生物学中试:花钱只是开始,烧钱才是常态

2026年合成生物学行业面临严峻产业化挑战:多家企业终止发酵项目或延期投产,显示从实验室到工厂的转化困境。核心问题并非基因合成成本,而是工艺放大难题——67%项目在中试阶段因工艺问题失败。AI辅助无法替代工程经验,菌株-配方-工艺的三元适配需要长期数据积累。中试成本惊人,涉及设备折旧、原料消耗和稀缺工艺人才,而行业普遍回避讨论这些工程现实,导致认知偏差。建议创业者提前布局中试,将其视为核心竞争力而非简单成本项。合成生物学的真正瓶颈在于工程化能力,这需要时间沉淀而非技术叙事。

AIexander的博客 942

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 re

weixin_45448563的博客 341

UE4_SpeedTree Shader源码分析

目录 1.UMaterialExpressionSpeedTree::Compile 2.Compiler->SpeedTree 3.GetSpeedTreeVertexOffset 4.Scene中的SpeedTree应用 (Scene->UpdateSpeedTreeWind) 5.GetSpeedTreeVertexOffsetInner 由于之后做风场,也需要在S...

zzgyy123的专栏 2631

Python小波变换实战:用PyCWT分析气候数据(附完整代码)

本文详细介绍了如何使用Python中的PyCWT库进行小波变换分析,特别针对气候数据(如NINO3海温异常数据)的多尺度周期性特征提取。通过完整的代码示例,从数据预处理、小波参数配置到结果可视化,逐步指导读者掌握小波变换在气候研究中的实际应用。

weixin_29088655的博客 282

牛客网--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 actuall...

weixin_41675900的博客 417

K8s(二十二):k8s集群如何部署一个Spring Boot项目(依赖于MySQL和Redis外部服务)

🔴 K8s(二十二):k8s集群如何部署一个Spring Boot项目(依赖于MySQL和Redis外部服务)

✨ 欢迎来到【Seal ^_^ 的优快云博客】!✨ 2万+

I want to go hone——变形的Dijkstra

题目描述    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 actu...

qq_38030194的博客 485

Xilinx FPGA平台GTX简易使用教程(四)GTX IP核配置教程

干货来了,GTX IP核配置,搬砖全靠它~~

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 1万+

openmediavault(OMV)(30)插件介绍(3)filebrowser文件管理器

filebrowser可以对omv的文件进行图形化管理,而不需要每次都通过命令进行修改。filebrowser安装。

Monster✺◟(∗❛ัᴗ❛ั∗)◞✺的博客 2321

KY28 I Wanna Go Home

【代码】KY28 I Wanna Go Home。

敲代码的小风 183

【AI智能体】Codex + Seedance2.5 制作AI视频实战操作详解

【AI智能体】Codex + Seedance2.5 制作AI视频实战操作详解

congge_study的博客 3818

ACM: I wanna go home

文章目录一、题目大意二、解题思路三、代码 一、题目大意 二、解题思路 两个阵营,只能越过一次阵营,因此把跨阵营的路建模为单向边即可 三、代码 #include<iostream> #include<vector> #include<queue> using namespace std; const int MAX = 606; struct Node {...

Wangpeiyi9979的博客 280

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

九度 OJ1162 I wanna go home

九度 OJ1162 I wanna go home 时间限制:1 秒 内存限制:32 兆 特殊判题:否 1.题目描述: The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant,...

qq_37053885的博客 278

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

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 actual...

姚军 324

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

[牛客][北大考研复试]I Wanna Go Home[dijkstra]

题目描述 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 bu...

weixin_30835923的博客 932

题目1162:I Wanna Go Home

#include "iostream" #include "stdio.h" #include #include #include #include #include #include #include #include #include #include using namespace std; //1162 const int INF=0x7fffffff;   struc

Aden的专栏 274

poj 3767 I Wanna Go Home

Description 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 politic

浮躁的人,终究一事无成! 1123

九度OJ-1162:I Wanna Go Home

使用的依然是Dijkstra算法的模板。略作修改即可 题目描述:     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

听风就是雨 463
上一篇: 最小化价格
下一篇: 出队
Go_Accepted
博客等级 码龄10年 601粉丝 334原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值