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>
#include <vector>
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
struct Edge{
int u, v, w;
int getTo(int u)
{
if(this->u == u) return this->v;
else return this->u;
}
}es[];
int n, m, q;
vector<int> arc[MAXN];
int d[MAXN], vis[MAXN], pre[MAXN], dp[MAXN][MAXN];
void prim(int src)
{
for(int i = ; i <= n; i++)
{
d[i] = INF;
vis[i] = ;
pre[i] = -;
for(int j = ; j <= n; j++)
{
dp[i][j] = ;
}
}
int t = n;
d[src] = ;
while(t--)
{
int mincost = INF, k;
for(int i = ; i <= n; i++)
{
if(!vis[i] && d[i] < mincost)
{
mincost = d[i];
k = i;
}
}
int fa = pre[k];
for(int i = ; i <= n; i++)
{
if(vis[i])
{
dp[i][k] = dp[k][i] = max(dp[i][fa], mincost);
}
}
vis[k] = ;
for(int i = , size = arc[k].size(); i < size; i++)
{
int id = arc[k][i];
int v = es[id].getTo(k);
if(!vis[v] && d[v] > es[id].w)
{
d[v] = es[id].w;
pre[v] = k;
}
}
}
}
int main()
{
while(scanf("%d %d %d", &n ,&m, &q) != EOF)
{
for(int i = ; i <= n; i++) arc[i].clear();
for(int i = ; i < m; i++)
{
scanf("%d %d %d", &es[i].u, &es[i].v, &es[i].w);
arc[es[i].u].push_back(i);
arc[es[i].v].push_back(i);
}
prim();
while(q--)
{
int id, x;
scanf("%d %d", &id, &x);
id--;
if(x <= dp[es[id].u][es[id].v])
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
return ;
}
POJ2831(次小生成树问题)的更多相关文章
- 2014.first[未填]
之后就按照自己的直觉,整理了第一套,难度为简单,差不多比2013noipday1水一点...先练练手而已 T1 vijos1196吃糖果游戏 博弈论 依题意,我们可知,如果去分数目为2,3,7,8必输 ...
- UVA-10600.Contest and Blackout.(Kruskal + 次小生成树)
题目链接 本题思路:模版的次小生成树问题,输出MST and Second_MST的值. 参考代码: #include <cstdio> #include <cstring> ...
- 次小生成树(Prim + Kruaskal)
问题引入: 我们先来回想一下生成树是如何定义的,生成树就是用n - 1条边将图中的所有n个顶点都连通为一个连通分量,这样的边连成子树称为生成树. 最小生成树很明显就是生成树中权值最小的生成树,那么我们 ...
- NetworkX系列教程(10)-算法之二:最小/大生成树问题
小书匠 Graph 图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定 ...
- 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态
最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- jQuery实践-网页版2048小游戏
▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...
- 微信小程序开发心得
微信小程序也已出来有一段时间了,最近写了几款微信小程序项目,今天来说说感受. 首先开发一款微信小程序,最主要的就是针对于公司来运营的,因为,在申请appid(微信小程序ID号)时候,需要填写相关的公司 ...
- 前端网络、JavaScript优化以及开发小技巧
一.网络优化 YSlow有23条规则,中文可以参考这里.这几十条规则最主要是在做消除或减少不必要的网络延迟,将需要传输的数据压缩至最少. 1)合并压缩CSS.JavaScript.图片,静态资源CDN ...
随机推荐
- Hadoop程序基础模板
分布式编程相对复杂,而Hadoop本身蒙上大数据.云计算等各种面纱,让很多初学者望而却步.可事实上,Hadoop是一个很易用的分布式编程框架,经过良好封装屏蔽了很多分布式环境下的复杂问题,因此,对普通 ...
- 【bzoj1040】骑士[ZJOI2008](树形dp)
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1040 这道题,很明显根据仇恨关系构造出的图形是一堆环套树.如果是普通的树,就可以马上裸树 ...
- 添加vue调试工具vue-devtolls
1.在使用脚手架vue-cli.js下载好node-modules 2.在node-modules目录下找的vue-devtools文件(如果没有可以用npm install vue-devtools ...
- Spring data jpa 实现简单动态查询的通用Specification方法
本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...
- springboot项目支持war部署tomcat
最近在学校spring boot 在网络上学校到简单的启动spring boot项目,也搭建好了,但时实际情况我的spring boot项目是要发布到tomcat中的,今天,随意打了个war包发布到t ...
- numpy array转置与两个array合并
我们知道,用 .T 或者 .transpose() 都可以将一个矩阵进行转置. 但是一维数组转置的时候有个坑,光transpose没有用,需要指定shape参数, 在array中,当维数>=2, ...
- FMX.TTabControl_多行
1. 重载 TTabControl.RealignTabs; 2. 3.
- ZC_注意点
1. domain类 里面的 属性的类型,一般都是用 包装类 2. 使用 "Hibernate Reverse Engineering ..." 来进行自动生成domain类和?? ...
- Python快速学习-函数
函数定义总结: 1.定义函数时,需要确定函数名和参数个数:2.如果有必要,先对参数的数据类型进行检查:3.函数体内部可以用return随时返回函数结果:4.函数执行完毕没有return语句时,自动re ...
- CLR事件与路由事件在XAML代码中应用时的区别
<Window x:Class="Demo_window.Window2"xmlns="http://schemas.microsoft.com/winfx/200 ...