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

来源:互联网 发布:矩阵的加法运算 编辑:程序博客网 时间:2024/06/17 00:52
/*
Can We Build This One?
Time Limit: 5000MS        Memory Limit: 65536K
Total Submissions: 1627        Accepted: 611
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.


题意:给你一幅图,再给你Q个询问,每个询问为id cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边

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

 首先肯定是构造一颗最小生成树,接下来就是枚举不在生成树里的边,假定为(u,v),此时应该把它加入到生成树中,但这样肯定会形成u->v的环路,
 此时肯定要删除u->v这条环路里的边,删哪一条呢?肯定是除了边(u,v)外的最大边。

现在是如何找到这条最大边,可以采用dp的思想,即dp[i][j]表示在树上i->j的最大值(注意,由于是树,肯定i->j的路径是唯一的)。
我们在做Prim算法时,每次都是加入一个顶点S,我们还应该记录下顶点S加入到生成树里,它与谁相连,即我们在更新cost[]数组时要记录的。这
样,我们可以得到状态方程:dp[i][j] = dp[j][i] = max(dp[j][pre[i]],cost[i]),此时i为即将要加入的点,而j是已经在生成树顶点集合里的点。

完成了一次Prim算法,同样也可以将dp数组更新好了,那么回到最开始的问题,删除的边我们就可以直接用dp[u][v]。


 看这位大神的博客懂的


#include <cstdio>#include <cstring>#include <algorithm> using namespace std;const int MAX = 1005;const int INF = 0x3f3f3f3f;bool vis[MAX];int pre[MAX];int cost[MAX];int map[MAX][MAX];int dp[MAX][MAX];int n,m,q;struct Node{int u;int v;int w;}edge[100000+10];void init()//初始化 {int i;for(i=1;i<=n;i++){cost[i] = map[1][i];pre[i] = 1;}memset(dp,0,sizeof(dp));memset(vis,false,sizeof(vis));}void add_edge(int u,int v,int w){   //构造图 if(w < map[u][v]){map[u][v] = map[v][u] = w;}}void Prim(){init();vis[1] = true;//int sum = 0;while(1){int u = -1,i,min = INF,v;for(i=1;i<=n;i++){if(!vis[i] && min>cost[i]){min = cost[i];u = i;}}vis[u] = true;if(u==-1){break;}//sum += cost[u];for(v=1;v<=n;v++){if(vis[v] && v!=u){   // 点u加入到生成树集合里,它与谁相连, 删(u,v)环路中 ,除了边(u,v)外的最大边dp[u][v] = dp[v][u] = max(dp[v][pre[u]],cost[u]); //u为即将要加入的点,而v是已经在生成树顶点集合里的点。}if(!vis[v] && cost[v]>map[u][v]){cost[v] = map[u][v];pre[v] = u;  //记录路径  v的前驱是u; }}} }int main(){while(scanf("%d%d%d",&n,&m,&q)!=EOF){int i,j;memset(map,0x3f,sizeof(map));for(i=1;i<=m;i++){scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);add_edge(edge[i].u,edge[i].v,edge[i].w); } Prim();int x,weight;while(q--){scanf("%d%d",&x,&weight);if(dp[edge[x].u][edge[x].v]>=weight){printf("Yes\n");}else{printf("No\n");}}}return 0;}


0 0