博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-3767 I Wanna Go Home -----有限制的dijkstra
阅读量:5905 次
发布时间:2019-06-19

本文共 2689 字,大约阅读时间需要 8 分钟。

I Wanna Go Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2481   Accepted: 1035

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 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?

Input

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.

Output

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.

Sample Input

211 2 1001 2331 2 1001 3 402 3 501 2 1553 1 2005 3 1502 5 1604 3 1704 2 1701 2 2 2 10

Sample Output

10090540
1 /* 功能Function Description:     POJ-3767  ----有限制的dijkstra 2    开发环境Environment:          DEV C++ 4.9.9.1 3    技术特点Technique: 4    版本Version: 5    作者Author:                   可笑痴狂 6    日期Date:                      20120821 7    备注Notes: 8         这道题想清楚了很简单,但是开始时候还是迷茫很久。 9         理解要点:10         1、永远不会走从领导2到领导1的路11         2、先根据题意建立双向路,然后把存在领导2到领导1的路去除,让其变成领导1到领导2的单项路,之后就是dijkstra了12 */13 #include
14 #include
15 #define inf 0x3fffffff16 int map[605][605];17 18 int dijks(int n)19 {20 int i,j,k,min;21 int dis[605],visit[605];22 memset(visit,0,sizeof(visit));23 visit[1]=1;24 for(i=1;i<=n;++i)25 dis[i]=map[1][i];26 for(i=1;i
dis[j])33 {34 min=dis[j];35 k=j;36 }37 }38 visit[k]=1;39 if(k==2)40 break;41 for(j=1;j<=n;++j)42 {43 if(!visit[j]&&dis[k]+map[k][j]

 

转载地址:http://qocpx.baihongyu.com/

你可能感兴趣的文章
人工智能让人类拥有怎样的未来,智能让每一天不一样
查看>>
ubuntu下开启crontab日志
查看>>
伪对象
查看>>
docker copy vs add
查看>>
msdba
查看>>
Linux的软件管理
查看>>
mac环境安装nginx
查看>>
Centos 7安装部署zabbix3.0实战(服务器端)
查看>>
基于node环境的一些新词语定义
查看>>
css3代码书写规范
查看>>
Oracle 11g物理Active Data Guard实时查询(Real-time query)特性
查看>>
python实战系列之RTX发送告警消息(四)
查看>>
理解 Glance - 每天5分钟玩转 OpenStack(20)
查看>>
用 ConfigMap 管理配置 - 每天5分钟玩转 Docker 容器技术(159)
查看>>
MySQL局域网访问配置
查看>>
我的友情链接
查看>>
eclipse 去掉 js 校验
查看>>
ASA防火墙5 安全context
查看>>
Selenium-Python小记(一)
查看>>
我最喜爱的新工具:Oracle VM VirtualBox
查看>>