博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3767
阅读量:6493 次
发布时间:2019-06-24

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

I Wanna Go Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2488   Accepted: 1041

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 //不存在从2到1的路径  2 #include 
3 #include
4 #define Max 0x7ffffff 5 int map[605][605]; 6 int lead[605]; 7 bool vis[605]; 8 int ans[605]; 9 int city,rode;10 void djk(int v)11 {12 memset(ans,0,sizeof(ans));13 memset(vis,0,sizeof(vis));14 int i,j,k,t,min = 0x7ffffff;15 int next;16 for(i=1;i<=city;i++)17 {18 ans[i] = map[v][i];19 //printf("%d\n",ans[i]);20 }21 vis[v] = 1;22 for(i=1;i<=city-1;i++)23 {24 min = 0x7ffffff;25 for(j=1;j<=city;j++)26 {27 if(!vis[j]&&min>ans[j])28 {29 next = j;30 min = ans[j];31 }32 }33 vis[next] = 1;34 for(k=1;k<=city;k++)35 if(!vis[k]&&(ans[k]>(min + map[next][k])))36 ////特别注意:不能定义为整形最大值 ,少了一个f就ac啦,因为 map[next][k]可能是整形最大值 ,再加上min就会溢出 37 ans[k] = min + map[next][k];38 }39 } 40 int main()41 {42 int i,j,k,t;43 int from,to,cost;44 while(scanf("%d%d",&city,&rode),city)45 {46 memset(lead,0,sizeof(lead));47 for(i=1;i<=city;i++)48 for(j=1;j<=city;j++)49 map[i][j] = map[j][i] = Max;50 for(i=1;i<=rode;i++)51 {52 scanf("%d%d%d",&from,&to,&cost);53 if(cost
=0)64 printf("%d\n",ans[2]);65 else66 printf("-1\n");67 }68 return 0;69 } 70 71

 

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

你可能感兴趣的文章
定制rpm包及搭建yum仓库
查看>>
zabbix 报警小案例
查看>>
CentOS 6.5下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器
查看>>
shell ping 网段 多进程(很简单,喜欢就拿去用)
查看>>
枚举类、单实例
查看>>
正则参考
查看>>
C语言及程序设计 实践项目——C语言程序初体验
查看>>
我的友情链接
查看>>
Windows下如何卸载一个服务
查看>>
使用Spring AOP切面解决数据库读写分离
查看>>
【java集合框架源码剖析系列】java源码剖析之HashSet
查看>>
《梦断代码》读书笔记一
查看>>
1_node.js
查看>>
图片压缩技术
查看>>
Exchange企业实战技巧(17)让密件抄送给特定用户
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
varnish学习笔记
查看>>
1.Phaser游戏引擎介绍
查看>>
队列的链式存储结构
查看>>