POJ2831(次小生成树问题)
Posted vCoders
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2831(次小生成树问题)相关的知识,希望对你有一定的参考价值。
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 1475 | Accepted: 546 | |
Case Time Limit: 2000MS |
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 the b-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), where N 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 integers a, b, and c (1 ≤ a, b ≤ N, a ≠ b, 0 ≤ c ≤ 1,000,000). The triplet (a, b, c) describes a path. Each of following Q lines contains two integer i and x (1 ≤ i ≤ M, 0 ≤ x) describing a query, “Can a highway be built along the i-th path if the cost of is reduced to x?” x is strictly lower than the original cost of building a highway along the i-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
思路:查询第i条边时,比较边的两个端点在(u,v)在树中的最长路与第i条边修改后值的大小。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN=1005; const int INF=0x3f3f3f3f; struct Node{ int u,v; }es[100005]; int tot; int arc[MAXN][MAXN]; int n,m,q; int d[MAXN],vis[MAXN],pre[MAXN]; int dp[MAXN][MAXN]; void prim(int src) { memset(pre,0,sizeof(pre)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { d[i]=INF; vis[i]=0; } d[src]=0; int t=n; while(t--) { int mincost=INF; int k; for(int i=1;i<=n;i++) { if(!vis[i]&&mincost>d[i]) { mincost=d[i]; k=i; } } vis[k]=1; int fa=pre[k]; for(int i=1;i<=n;i++) { if(vis[i]&&i!=k) { dp[k][i]=dp[i][k]=max(dp[i][fa],mincost); } } for(int i=1;i<=n;i++) { if(!vis[i]&&d[i]>arc[i][k]) { d[i]=arc[i][k]; pre[i]=k; } } } } int main() { while(scanf("%d%d%d",&n,&m,&q)!=EOF) { tot=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i==j) arc[i][j]=0; else arc[i][j]=INF; for(int i=0;i<m;i++) { int w; ++tot; scanf("%d%d%d",&es[tot].u,&es[tot].v,&w); arc[es[tot].u][es[tot].v]=arc[es[tot].v][es[tot].u]=min(arc[es[tot].v][es[tot].u],w); } prim(1); while(q--) { int id,w; scanf("%d%d",&id,&w); if(w<=dp[es[id].u][es[id].v]) { printf("Yes\n"); } else { printf("No\n"); } } } return 0; }
以上是关于POJ2831(次小生成树问题)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 1679 The Unique MST(次小生成树)
POJ - 1679 The Unique MST (次小生成树)