关闭

poj 2831 Can We Build This One?

164人阅读 评论(0) 收藏 举报
分类:
POJ - 2831
Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

Status

Description

“Highways are built, then life is rich.” Now people of Big Town want to become rich, so they are planning to build highways to connect their villages.

Big Town is really big and has many villages. Its people plan to build some highways between some pairs of villages so that every pair of villages is connected by the highways either directly or indirectly. After surveying the geographical surroundings, they find that there are some paths along with highways can be built. Every path is denoted by a triplet (a,b, c) which means a highway can built between the a-th village and theb-th village with a cost of c. In order to save money, they will select only part of the paths to build highways along so that the total cost to build highways along the selected paths is minimal under the condition that every pair of villages is connected.

It is possible that multiple such selections exist. People from every village want to have those highways of good interest to them built. But some highways can never appear in the selection since they are much too costly. So people ask whether a certain highway can be selected if they agree to cut the cost. Your task is to design a program to answer their queries.

Input

The first line of input contains three integers N, M and Q (1 < N ≤ 1,000, N − 1 ≤ M ≤ 100,000, 0 < Q ≤ 100,000), whereN is the number of villages, M is the number of paths, and Q is the number of queries. Each of the next M lines contains three integersa, b, and c (1 ≤ a, bN, ab, 0 ≤ c ≤ 1,000,000). The triplet (a, b,c) describes a path. Each of following Q lines contains two integeri and x (1 ≤ iM, 0 ≤ x) describing a query, “Can a highway be built along thei-th path if the cost of is reduced to x?” x is strictly lower than the original cost of building a highway along thei-th path. It is assumed that every pair of village will be connected either directly or indirectly if all possible highways are built. And there may be more than one highway that can be built between a pair of villages.

Output

Output one line for each query. Output either “Yes” or “No” as the answer to the the query.

Sample Input

3 4 3
1 2 10
1 3 6
2 3 4
1 3 7
4 6
1 7
1 5

Sample Output

Yes
No
Yes

题意;

给你一幅图,再给你Q个询问

每个询问为id c

即如果将id这条边的边权改为c的话,这条边是否可能是最小生成树中的一条边


做法: 其他的做法都是用pime变形,我作为一个只会写kruscal的可怜的人,提供kruscal做法。。。

就是在求完最小生成树后,算一个D【i】【j】表示i与j之间的最长边的长度。查询取出u, v,看D[u][v]是

不是小于等于给定值即可。


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;

#define MAXN (1000+5)
#define MAXM (100000+5)

struct node{
	int u, v, dis;
	
	bool operator <(const node &x)const{
		return dis < x.dis;
	};
};

int n, m, q, tn;
node edges[MAXM], red[MAXM];
vector<node> tree[MAXN];
int f[MAXN], D[MAXN][MAXN];
bool vis[MAXN];

int find(int x){
	return x==f[x]? x: f[x] = find(f[x]);
}

void Bspfa(int s){
	memset(vis, 0, sizeof(vis));
	queue<int> q;
	
	q.push(s); D[s][s] = 0; vis[s] = true;
	while(!q.empty()){
		int now = q.front(); q.pop();
		
		for(int i = 0; i < tree[now].size(); i++){
			node next = tree[now][i];
			int v = tree[now][i].v, dis = tree[now][i].dis;
			
		//	printf("now = %d v = %d dis = %d\n", now, v, dis);
			if(vis[v]) continue;
		//	printf("in!\n");
			
			D[s][v] = max(D[s][now], dis);
		//	printf("D[%d][%d] = %d\n", s, v, D[s][v]);
			q.push(v);
			vis[v] = true;
		}
	}
}

void kruscal(){
	for(int i = 1; i <= n; i++) f[i] = i;
	
	int tot = 0;
	for(int i = 1; i <= m; i++){
		int u = edges[i].u, v = edges[i].v, dis = edges[i].dis;
		int x = find(u), y = find(v);
		
		if(x == y) continue;
		f[x] = y;
		tree[u].push_back((node){u, v, dis});
		tree[v].push_back((node){v, u, dis});
	//	printf("u = %d v = %d dis = %d\n", u, v, dis);
		
		if(++tot == n-1) break;
	}
	
	for(int i = 1; i <= n; i++) Bspfa(i);
}

int main(){
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
	
	scanf("%d%d%d", &n, &m, &q);
	
	for(int i = 1; i <= m; i++){
		int u, v, d;
		scanf("%d%d%d", &u, &v, &d);
		
		edges[i] = (node){u, v, d};
		red[i] = (node){u, v, d};
	}
	sort(edges+1, edges+m+1);
	
	kruscal();
	
	for(int i = 1; i <= q; i++){
		int num, newd;
		scanf("%d%d", &num, &newd);
		
		int u = red[num].u, v = red[num].v;
		if(D[u][v] >= newd) printf("Yes\n");
		else printf("No\n");
	}
	
	return 0;
}

pime算法好短的,见段大神的(http://blog.csdn.net/dy0607/article/details/52372955)

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9010次
    • 积分:875
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:0篇
    • 译文:0篇
    • 评论:19条
    另一个我
    Free组合~~友链O(∩_∩)O
    文章分类