poj 2831 Can We Build This One?

288人阅读 评论(0) 收藏 举报
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    ...
  • huangshuai147
  • huangshuai147
  • 2017年04月15日 11:11
  • 397

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

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

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

Can We Build This One? Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 141...
  • KeyboardMagician
  • KeyboardMagician
  • 2016年03月21日 22:34
  • 318

POJ 2831 次小生成树

题意:给你n个点,m个遍,q次询问。问你改了第i条边的权值,他能否使得最小价格降低,或不变。 想法:是次小生成树的变形,很简单,但是我被prime恶心死了。 #include #include...
  • Triple_WDF
  • Triple_WDF
  • 2015年11月07日 14:29
  • 255

poj 2831(次小生成树)

题意:给你一幅图,再给你Q个询问,每个询问为id cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边 解题思路:将第i条边(u,v)的权值修改的话,要判断是...
  • hexianhao
  • hexianhao
  • 2016年05月10日 17:42
  • 801

poj 2831 Can We Build This One?

POJ - 2831 Can We Build This One? Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format...
  • miaomiao_ymxl
  • miaomiao_ymxl
  • 2016年08月30日 20:50
  • 288


序号 题目 AC率 通过数 提交数 1000 A+B Problem 0.55 188072 338977 1004 Financial Management 0.41 58282 14030...
  • Sureina
  • Sureina
  • 2016年08月08日 08:57
  • 2943

POJ 2831:Can We Build This One?

Can We Build This One? Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: ...
  • u010885899
  • u010885899
  • 2016年05月01日 22:59
  • 225

【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
  • 304

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

题意: 某国计划修建若干高速公路,用来连接国内N个城市,经过一番细致的考察后,政府迁出了M条待建的公路  每条公路用三个整数(x,y,z)来,即城市X与城市Y之间可以修一条高速公路,需要Z的花费。...
  • rachelsg
  • rachelsg
  • 2016年04月20日 23:16
  • 183
    访问量: 1万+
    积分: 950
    排名: 5万+