题目描述:

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
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Scanner;
import java.util.Queue;
import java.util.Arrays;
import java.util.LinkedList;class Main
{public static final boolean DEBUG = false;public static int N = 610;;public static int[][] f;public static int n;public static int[] leader;public static void bfs(){Queue<Integer> q = new LinkedList<Integer>();q.add(Integer.valueOf(1));int[][] dis = new int[n + 1][2];for (int i = 1; i <= n; i++) {dis[i][0] = dis[i][1] = Integer.MAX_VALUE;}dis[1][0] = dis[1][1] = 0;while (!q.isEmpty()) {int u = q.poll().intValue();for (int v = 1; v <= n; v++) {if (f[u][v] != 0) {boolean ok = false;if (leader[u] == leader[v]) {if (dis[u][0] != Integer.MAX_VALUE && dis[u][0] + f[u][v] < dis[v][0]) {dis[v][0] = dis[u][0] + f[u][v];ok = true;} if (dis[u][1] != Integer.MAX_VALUE && dis[u][1] + f[u][v] < dis[v][1]) {dis[v][1] = dis[u][1] + f[u][v];ok = true;}} else {if (dis[u][0] != Integer.MAX_VALUE && dis[u][0] + f[u][v] < dis[v][1]) {dis[v][1] = dis[u][0] + f[u][v];ok = true;} }if (ok) q.add(Integer.valueOf(v));}}}int ans = Integer.MAX_VALUE;for (int i = 0; i < 2; i++) {ans = Math.min(dis[2][i], ans);}if (ans == Integer.MAX_VALUE) System.out.println(-1);else System.out.println(ans);}public static void main(String[] args) throws IOException{Scanner cin;int m;if (DEBUG) {cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));} else {cin = new Scanner(new InputStreamReader(System.in));}f = new int[N][N];while (cin.hasNext()) {n = cin.nextInt();if (n == 0) break;m = cin.nextInt();for (int i = 0; i < N; i++) Arrays.fill(f[i], 0);//System.out.println("n = " + n + " m = " + m);for (int i = 0; i < m; i++) {int u, v, w;u = cin.nextInt();v = cin.nextInt();w = cin.nextInt();f[u][v] = f[v][u] = w;}leader = new int[n + 1];for (int i = 1; i <= n; i++) leader[i] = cin.nextInt();bfs();}}
}

题目1162:I Wanna Go Home相关推荐

  1. 题目 1162: 密码

    网上流传一句话:"常在网上飘啊,哪能不挨刀啊-".其实要想能安安心心地上网其实也不难,学点安全知识就可以. 首先,我们就要设置一个安全的密码.那什么样的密码才叫安全的呢?一般来说一 ...

  2. 2011年北京大学计算机研究生机试真题(dijkstra+优先队列)

    http://ac.jobdu.com/problem.php?pid=1162  I Wanna Go Home 方法一:普通的dijkstra /* 很明显的最短路,但关键是如何建图.可以看到,一 ...

  3. I wanna go home

    题目网址:I wanna go home 题目大意:给出n个城市 ,给出m对城市之间的距离,再给出每一个城市的所属的阵营,要求求出从城市1到城市2的最小的路径长度,城市1总是属于阵营1的,城市2是属于 ...

  4. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

  5. HDU 5308 I Wanna Become A 24-Point Master

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5308 题面: I Wanna Become A 24-Point Master Time Limit ...

  6. HDU题目分类啊!!!

    分类一(详细): 分类二: 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.10 ...

  7. 信息学奥赛一本通 1162:字符串逆序

    [题目链接] ybt 1162:字符串逆序 [题目考点] 1. 字符串 2. 递归 [解题思路] 先去掉字符串末尾的'!' 递归输出逆序字符串 递归问题:输出字符串s的逆序字符串 递归关系:设字符串s ...

  8. 信息学奥赛一本通(1162:字符串逆序)

    1162:字符串逆序 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 14027     通过数: 10525 [题目描述] 输入一串以'!'结束的字符,按逆序输出 ...

  9. HDU 1162 Eddy's picture (最小生成树)(java版)

    Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 --每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...

最新文章

  1. 柜子里的人笑出了声...
  2. Zuul:智能路由和过滤(译)
  3. Ghost配置1——删除社交Link
  4. 39个转录组分析工具,120种组合评估
  5. redis两种持久化模式
  6. 无法从Windows上的资产index.android.bundle加载脚本
  7. php中的分页类Page的用法
  8. 如何正确预防网页中的5种“隐形杀手”
  9. 给初学Python的小伙伴一些建议
  10. 财源滚滚的第三方支付牌照
  11. 搜索及代码在GitHub上查重小技巧
  12. 好家伙?阿里网盘的分享功能已经出来了?
  13. flyingsaucer转换多个html,Flying Saucer实现html转pdf(一些有关问题,持续更新)
  14. 图像增强算法(持续更新中)
  15. 用论文写作平台Overleaf写中文论文
  16. 黑客挂马紧盯娃娃 儿童节育儿教育网站被挂马
  17. 平面Photoshop制作精致的早餐油条…
  18. 职场吐槽大会,原来办公软件也有如此多神技能?
  19. 阿里云盘承诺永不限速
  20. 8/21 牛客补题+cf思维+tarjan

热门文章

  1. 微软图表控件MsChart使用说明[转]
  2. 利用LSM实现更安全的linux
  3. 硬盘最多能分几个区?
  4. python自动化测试看什么书-《自动化平台测试开发-Python测试开发实战》新书出版了...
  5. python简单代码 春节集五福-集五福活动又来了,不过这个价值几十亿的大项目也别错过...
  6. 没有任何基础的可以学python吗-今天就来告诉你,没有编程基础的人适不适合学python...
  7. python是个啥-Python是个什么鬼?师兄用它一年发表5篇SCI!
  8. python爬虫入门实例-Python-入门的第一个爬虫例子
  9. python英语字典程序-Pyqt5实现英文学习词典
  10. python详细安装教程3.7.4-python 3.7.4 安装 opencv的教程