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 查看评论

[原]武大预选赛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. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  2. 【BZOJ】【1044】【HAOI2008】木棍分割

    二分/DP 真是一道好题! 第一问很简单的二分…… 第二问一开始我想成贪心了,其实应该是DP的= = 然后没有注意……又MLE又TLE的……这题要对DP进行时空两方面的优化!! 题解:(by JoeF ...

  3. 使用Provider时提示:Unable to get provider...

    具体原因还不清楚,只是找到了原因: 这是因为自定义的Provider放的包路径不对,自定义的Provider应该放到和MainActivity同一个包中.

  4. 跨站点端口攻击 – XSPA(SSPA)

    许多Web应用程序提供的功能将数据从其他Web服务器,由于种种原因.下载XML提要,从远程服务器,Web应用程序可以使用用户指定的URL,获取图像,此功能可能会被滥用,使制作的查询使用易受攻击的Web ...

  5. JavaScript之四种继承方式讲解

    在Javascript中,所有开发者定义的类都可以作为基类,但出于安全性考虑,本地类和宿主类不能作为基类,这样可以防止公用访问编译过的浏览器级的代码,因为这些代码可以被用于恶意攻击. 选定基类后,就可 ...

  6. ASP.NET MVC与RAILS3的比较

    进入后Web年代之后,MVC框架进入了快速演化的时代,Struts等垂垂老矣的老一代MVC框架因为开发效率低下而逐渐被抛弃,新一代的MVC则高举敏捷的大旗,逐渐占领市场,其中的代表有Rails (ru ...

  7. 腾讯开源的轻量级CSS3动画库:JX.Animate

          JX.Animate 是由腾讯前端团队 AlloyTeam 推出的一个 CSS3 动画库,通过 JX(腾讯的前端框架)插件的形式提供. Why CSS3 众所周知在支持HTML5的浏览器中 ...

  8. HDU 4572 Bottles Arrangement(找规律,仔细读题)

    题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1 ...

  9. URAL 1167. Bicolored Horses (DP)

    题目链接 题意 :农夫每天都会放马出去,然后晚上把马赶入马厩,于是让马排成一行入马厩,但是不想马走更多的路,所以让前p1匹入第一个马厩,p2匹马入第二个马厩…………但是他不想让他的任何一个马厩空着,所 ...

  10. ExtJs之Ext.util.CSS

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...