poj 2831次小生成树变形
来源:互联网 发布:淘宝买东西不给发票 编辑:程序博客网 时间:2024/06/08 06:11
题目:
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 31 2 101 3 62 3 41 3 74 61 71 5
Sample Output
YesNoYes
给一幅图和Q个询问,每个询问为id cost,即如果将id这条边的边权改为cost的话,这条边是否可能是最小生成树中的一条边
代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int maxn=1010;const int maxm=100010;const int inf=0x7f7f7f7f;struct Edge{int u,v,c;}E[maxm];///只参与输入输出处理int n,m,q;int G[maxn][maxn],path[maxn][maxn];///图 生成树中两点间最大边权int pre[maxn],low[maxn];bool vis[maxn],used[maxn][maxn];///点 边使用情况int Prim(){int k = 1,ans = 0;///向生成树中加的点memset(path,0,sizeof(path));memset(vis,false,sizeof(vis));memset(used,false,sizeof(used));vis[k] = true;for(int i = 1; i <= n; i++){///点的编号从1开始low[i] = G[k][i];pre[i] = k;}for(int i = 1; i < n; i++){int MIN = inf;for(int j = 1; j <= n; j++){if(vis[j] == false && low[j] < MIN){MIN = low[j];k = j;}}ans += MIN;vis[k] = true;used[k][pre[k]] = used[pre[k]][k] = true;for(int j = 1; j <= n; j++){if(vis[j] == true && j != k) path[j][k] = path[k][j] = max(path[j][pre[k]],low[k]);if(vis[j] == false && low[j] > G[k][j]){low[j] = G[k][j];pre[j] = k;}}}return ans;}int main(){///10872K1750MSwhile(scanf("%d%d%d",&n,&m,&q)==3){memset(G,inf,sizeof(G));for(int i = 1; i <= m; ++i){scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].c);G[E[i].u][E[i].v] = G[E[i].v][E[i].u] = min(G[E[i].u][E[i].v],E[i].c);}int ans = Prim();while(q--){int id,cost;scanf("%d%d",&id,&cost);if(path[E[id].u][E[id].v] >= cost) printf("Yes\n");else printf("No\n");}}return 0;}
- poj 2831次小生成树变形
- hdu4081 次小生成树变形
- poj 2831 次小生成树
- poj 2831 次小生成树模板
- POJ 2831 次小生成树
- poj 2831(次小生成树)
- poj 1679 The Unique MST(次小生成树变形)
- 修路方案 【最小生成树变形】-- 次小生成树
- HDU 4081 次小生成树变形记
- POJ 1679 次小生成树
- poj 1673 次小生成树
- poj 1679 最小 次小生成树
- 次小生成树 (poj 1679)
- POJ 1679 次小生成树
- 次小生成树 POJ 1679
- 次小生成树POJ 1679
- poj 1679 次小生成树
- poj 1679(次小生成树)
- Codeforces Round #427 (Div. 2)
- 动态规划-背包问题(给定容量获得最大的价值量)1
- redis备份恢复-物理备份迁移集群
- 链式前向星--最小斯坦纳树
- JAVA——构造函数
- poj 2831次小生成树变形
- 计算机网络各层的功能作用
- 《AutoCAD 基本操作》
- 沉浸式状态栏相关
- 【笔记】有点麻烦的MatConvNet的dagnn的debug过程
- iptables学习笔记:端口转发之“内网访问外网”
- session的序列化以及session的钝化和活化
- poj 1022 Packing Unit 4D Cubes 搜索
- 文章标题