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



“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.


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 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




每个询问为id c


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

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



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;
		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]);
			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);
	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;



POJ_2831_Can We Build This One?【次小生成树】

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

POJ 2831:Can We Build This One?

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

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

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

【POJ 2831】 Can We Build This One?(prim 最小生成树变形)

Description 某国计划修建若干高速公路,用来连接国内N个城市,经过一番细致的考察后,政府迁出了M条待建的公路  每条公路用三个整数(x,y,z)来,即城市X与城市Y之间可以修一条高速公路,...
  • FK_ACM
  • FK_ACM
  • 2016年04月11日 12:07
  • 276

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

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

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

题意: 某国计划修建若干高速公路,用来连接国内N个城市,经过一番细致的考察后,政府迁出了M条待建的公路  每条公路用三个整数(x,y,z)来,即城市X与城市Y之间可以修一条高速公路,需要Z的花费。...

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 2028 When Can We Meet?(我的水题之路——边输入,边搜索)

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

POJ2028——When Can We Meet?

When Can We Meet? Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5461 ...
  • LDUtyk
  • LDUtyk
  • 2016年09月11日 00:11
  • 347

北大ACM poj2028 When Can We Meet?

When Can We Meet?   Description The ICPC committee would like to have its meeting as soon as poss...
您举报文章:poj 2831 Can We Build This One?