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代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define INF1 1000000000000000001
#define INF2 1000000009
const int maxn = 100010;
struct node{
int s,e,w;
}map[maxn];
long long d[201][201];
long long fa[maxn];
int n, m;
int h[maxn];
void init(int a)
{
for(int i = 0; i <= a; i++)
{
fa[i] = i;
h[i] = 0;
}
for(int i = 0; i <= a; i++){
map[i].w = INF2;
}
} int Find(int x)
{
if(fa[x] == x)
return x;
else
return fa[x] = Find(fa[x]);
} void unite(int x,int y)
{
x = Find(fa[x]);
y = Find(fa[y]); if(x == y) return;
else
{
if(h[x] > h[y]) fa[y] = fa[x];
else
{
fa[x] = fa[y];
if(h[x] == h[y]) h[y]++;
}
}
}
bool M[maxn];
int s[maxn];
int r;
void discretize(){
memset(M, false, sizeof(M));
//memset(s, 0, sizeof(s));
int k;
for(int i = 1; i <= n; i++){
k = Find(i);
if(!M[k]){
s[k] = r++;
M[k] = true;
}
}
for(int i = 1; i < r; i++){
for(int j = 1; j < r; j++){
if(i == j) d[i][j] = 0;
else d[i][j] = INF1;
}
}
for(int j = 1; j <= m; j++){
if(map[j].w != 0){
int dx = Find(map[j].s);
int dy = Find(map[j].e);
dx = s[dx];
dy = s[dy];
d[dx][dy] = d[dy][dx] = min(d[dx][dy],(long long)map[j].w);
//cout<<"*"<<d[dx][dy]<<"*"<<endl;
}
}
} void floyd(int r){
for(int k = 1; k < r; k++)
for(int i = 1; i < r; i++)
for(int j = 1; j < r; j++){
if(d[i][k] < INF1 && d[k][j] < INF1) d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
}
}
void input(int m){
for(int i = 1; i <= m; i++){
int a , b;
scanf("%d%d",&map[i].s, &map[i].e);
scanf("%d",&map[i].w);
if(map[i].w == 0) unite(map[i].s, map[i].e);
}
} int main(){
while(cin>>n>>m&&n){
init(n);
input(m); r = 1;
discretize();
floyd(r); int q;
cin>>q;
while(q--){
int a, b;
scanf("%d%d",&a, &b);
a = Find(a);
b = Find(b);
a = s[a];
b = s[b];
if(d[a][b] == INF1) printf("-1\n");
else
printf("%lld\n",d[a][b]);
}
}
}

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

最新文章

  1. 简单的HttpClient使用
  2. GJM:C# WinForm开发系列 - DataGridView 使用方法集锦 [转载]
  3. RabbitMQ集群环境搭建-4
  4. Mac添加bash alias
  5. destoon 深度整合discuz x2 UC 之免邮箱二次验证
  6. android基础知识13:AndroidManifest.xml文件解析
  7. div的contenteditable和placeholder蹦出的火花
  8. selenium+python登录登出百度,等待页面加载,鼠标定位
  9. Ubuntu14.04安装Oracle12C
  10. 【踩坑记】从HybridApp到ReactNative
  11. UVa 673 Parentheses Balance(栈的使用)
  12. 走进C标准库(3)——&quot;stdio.h&quot;中的getc和ungetc
  13. Knockout应用开发指南 第三章:绑定语法(1)
  14. Java容器的概要
  15. Centos7 上安装配置 RabbitMQ
  16. Oracle 12C CRS-5013
  17. 在chrome上隐藏video的option按钮
  18. 十六、Mediator 仲载者设计模式
  19. Hexo + Github 个人博客设置以及优化
  20. MFC入门(一)-- 第一个简单的windows图形化界面小程序(打开计算器,记事本,查IP)

热门文章

  1. java 验证身份证号
  2. Shiro workshop
  3. destoon使用中的一些心得
  4. Detecting diabetic retinopathy in eye images
  5. Codeforces Round #268 (Div. 2)
  6. Codeforces 322B
  7. wordpress无法安装这个包。: PCLZIP_ERR_MISSING_FILE (-4) : Missing archive file &#39;C:\WINDOWS\TEMP/wordpress-4.tmp&#39;
  8. String、StringBuilder
  9. 【leetcode】Multiply Strings(middle)
  10. iOS开发--动画(Animation)总结