Problem 1542 - F - Countries

Time Limit: 1000MS Memory Limit: 65536KB

Total Submit: 266 Accepted: 36 Special Judge: No

Description

There are n countries at planet X on which Xiao Ming was born.





Some countries, which have been sharing fine bilateral relations, form a coalition and thus all of their citizens will benefit from a policy for which all the travels between these countries will become totally free.



But it is not easy to travel between countries distributed in different coalitions. If possible, it must cost some money called YZB(yu zhou bi) which is always positive.



Help Xiao Ming determine the minimum cost between countries.

Input

The input consists of one or more test cases.



First line of each test case consists two integers n and m. (1<=n<=10^5, 1<=m<=10^5)



Each of the following m lines contains: x y c, and c indicating the YZB traveling from x to y or from y to x. If it equals to zero, that means x and y are in the same coalition. (1<=x, y<=n, 0<=c<=10^9)

You can assume that there are no more than one road between two countries.



Then the next line contains an integer q, the number of queries.(1<=q<=200)



Each of the following q lines contains: x y. (1<=x, y<=n)



It is guaranteed that there are no more 200 coalitions.



Input is terminated by a value of zero (0) for n.

Output

For each test case, output each query in a line. If it is impossible, output “-1”.



Sample Input

6 5

1 2 0

2 4 1

3 5 0

1 4 1

1 6 2

3

4 2

1 3

4 6

0

Sample Output

1

-1

3

AC代码如下:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. using namespace std;
  6. #define INF1 1000000000000000001
  7. #define INF2 1000000009
  8. const int maxn = 100010;
  9. struct node{
  10. int s,e,w;
  11. }map[maxn];
  12. long long d[201][201];
  13. long long fa[maxn];
  14. int n, m;
  15. int h[maxn];
  16. void init(int a)
  17. {
  18. for(int i = 0; i <= a; i++)
  19. {
  20. fa[i] = i;
  21. h[i] = 0;
  22. }
  23. for(int i = 0; i <= a; i++){
  24. map[i].w = INF2;
  25. }
  26. }
  27.  
  28. int Find(int x)
  29. {
  30. if(fa[x] == x)
  31. return x;
  32. else
  33. return fa[x] = Find(fa[x]);
  34. }
  35.  
  36. void unite(int x,int y)
  37. {
  38. x = Find(fa[x]);
  39. y = Find(fa[y]);
  40.  
  41. if(x == y) return;
  42. else
  43. {
  44. if(h[x] > h[y]) fa[y] = fa[x];
  45. else
  46. {
  47. fa[x] = fa[y];
  48. if(h[x] == h[y]) h[y]++;
  49. }
  50. }
  51. }
  52. bool M[maxn];
  53. int s[maxn];
  54. int r;
  55. void discretize(){
  56. memset(M, false, sizeof(M));
  57. //memset(s, 0, sizeof(s));
  58. int k;
  59. for(int i = 1; i <= n; i++){
  60. k = Find(i);
  61. if(!M[k]){
  62. s[k] = r++;
  63. M[k] = true;
  64. }
  65. }
  66. for(int i = 1; i < r; i++){
  67. for(int j = 1; j < r; j++){
  68. if(i == j) d[i][j] = 0;
  69. else d[i][j] = INF1;
  70. }
  71. }
  72. for(int j = 1; j <= m; j++){
  73. if(map[j].w != 0){
  74. int dx = Find(map[j].s);
  75. int dy = Find(map[j].e);
  76. dx = s[dx];
  77. dy = s[dy];
  78. d[dx][dy] = d[dy][dx] = min(d[dx][dy],(long long)map[j].w);
  79. //cout<<"*"<<d[dx][dy]<<"*"<<endl;
  80. }
  81. }
  82. }
  83.  
  84. void floyd(int r){
  85. for(int k = 1; k < r; k++)
  86. for(int i = 1; i < r; i++)
  87. for(int j = 1; j < r; j++){
  88. if(d[i][k] < INF1 && d[k][j] < INF1) d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
  89. }
  90. }
  91. void input(int m){
  92. for(int i = 1; i <= m; i++){
  93. int a , b;
  94. scanf("%d%d",&map[i].s, &map[i].e);
  95. scanf("%d",&map[i].w);
  96. if(map[i].w == 0) unite(map[i].s, map[i].e);
  97. }
  98. }
  99.  
  100. int main(){
  101. while(cin>>n>>m&&n){
  102. init(n);
  103. input(m);
  104.  
  105. r = 1;
  106. discretize();
  107. floyd(r);
  108.  
  109. int q;
  110. cin>>q;
  111. while(q--){
  112. int a, b;
  113. scanf("%d%d",&a, &b);
  114. a = Find(a);
  115. b = Find(b);
  116. a = s[a];
  117. b = s[b];
  118. if(d[a][b] == INF1) printf("-1\n");
  119. else
  120. printf("%lld\n",d[a][b]);
  121. }
  122. }
  123. }

作者:u011652573 发表于2014-4-1 15:55:37 原文链接
阅读:58 评论:0 查看评论

[原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)的更多相关文章

  1. AtCoder Beginner Contest 247 F - Cards // dp + 并查集

    原题链接:F - Cards (atcoder.jp) 题意: 给定N张牌,每张牌正反面各有一个数,所有牌的正面.反面分别构成大小为N的排列P,Q. 求有多少种摆放方式,使得N张牌朝上的数字构成一个1 ...

  2. [Comet OJ - Contest #6 D][48D 2280]另一道树题_并查集

    另一道树题 题目大意: 数据范围: 题解: 这个题第一眼能发现的是,我们的答案分成两种情况. 第一种是在非根节点汇合,第二种是在根节点汇合. 尝试枚举在第几回合结束,假设在第$i$回合结束的方案数为$ ...

  3. 【POJ】The Suspects(裸并查集)

    并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...

  4. zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)

    题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...

  5. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  6. POJ 2524 独一无二的宗教(裸并查集)

    题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K ...

  7. Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)

    题目链接  2016 ACM-ICPC EC-Final Problem G 题意  给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...

  8. hiho 171周 - 水题,并查集

    题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...

  9. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

随机推荐

  1. PHP中如何连接数据库基本语句

    只是后端修改页面,不需要在前端显示的可以删除原有代码只输入<?php 开始编写语言即可,后面的?>也可以省略 //造一个连接$connect = @mysql_connect(" ...

  2. 【CTSC 2015】&【APIO 2015】酱油记

    蒟蒻有幸参加了神犇云集的CTSC & APIO 2015,感觉真是被虐成傻逼了……这几天一直没更新博客,今天就来补一下吧~~(不过不是题解……) Day 0 从太原到北京现在坐高铁只需3小时= ...

  3. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. C#获取网页中的验证码图片(转载)

    有时候我们需要获得网页上的图片,尤其是向验证码这样的图片.这个方法就是将网页上的图片获取到PictureBox中.效果入下图所示. 右边是使用Webbrowser控件装载的某网站的注册页面,其中包括了 ...

  5. 一系列JavaScript的基础工具

    在我们的bootcamp训练营中,学员们介绍了一些工具和库来扩展他们代码的能力.Kalina,目前我们JavaScript学员中的一员,列举了这些工具,想和其它爱好代码的小伙伴一起分享. 点击看大图 ...

  6. sql server 2008 执行计划

    SSMS允许我们查看一个图形化的执行计划(快捷键Ctrl+L)

  7. 用 VIPER 构建 iOS 应用架构(2)

    [编者按]本篇文章由 Jeff Gilbert 和 Conrad Stoll 共同编写,通过构建一个基础示例应用,深入了解 VIPER,并从视图.交互器等多个部件理清 VIPER 的整体布局及思路.通 ...

  8. 请教DotNetBar控件中的CalendarView控件如何拖动当前的时间轴

    本人想拖动那个当前的时间轴或者让时间轴变动,因为那个时间轴默认的是当前时间.(就是那个黄色的线)

  9. 【二】php常用方法

    -------------------------------数据类型------------------------------------------ 1.settype(var,type)  类 ...

  10. POJ 2549

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8235   Accepted: 2260 Descripti ...