poj 2831 Can We Build This One?

原创 2016年08月30日 20:50:55
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)

版权声明:支持的都转走!!!

相关文章推荐

POJ2831 Can We Build This One?(次小生成树)

Can We Build This One? Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 141...

POJ 2028 When Can We Meet?(我的水题之路——边输入,边搜索)

When Can We Meet? Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3489 ...

北大ACM poj2028 When Can We Meet?

When Can We Meet?   Description The ICPC committee would like to have its meeting as soon as poss...

POJ2028——When Can We Meet?

When Can We Meet? Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5461 ...

POJ 2028 When Can We Meet? 枚举

When Can We Meet? Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5525 ...

POJ 2028 When Can We Meet?(模拟)

When Can We Meet? Description The ICPC committee would like to have its meeting as soon as possi...

poj解题报告——poj 2028 When Can We Meet?

原题入口poj 2028 When Can We Meet? The ICPC committee would like to have its meeting as soon as possible...

Poj2831 树链剖分||次小生成树

因为开的树链剖分专题,一看题目,第一思路就是: 让找出一个最小生成树,然后将这棵生成树进行链剖,再维护两点间边的最大值,只有查询询问,很好写,然后由于太急躁,以致Find()函数明显的错误居然没有发...

poj 2831(次小生成树)

题意:给你一幅图,再给你Q个询问,每个询问为id cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边 解题思路:将第i条边(u,v)的权值修改的话,要判断是...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)