POJ 2831:Can We Build This One?
2016-05-01 22:59
369 查看
Can We Build This One?
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
Sample Output
题意是询问一个边变小之后能不能进入到最小生成树的边中。
求最小生成树,计算两个点在最小生成树中的最大边,如果减过之后的边比这个最大边都小,那就可以替换到最小生成树中。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x3fffffff
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
const int maxn = 1e3 + 5;
const ll mod = 1000000;
const double PI = acos(-1.0);
int n, m, q;
int mini[maxn], vis[maxn], pre[maxn];
int dis[maxn][maxn], res[maxn][maxn];
struct ed
{
int u;
int v;
int w;
}edge[200005];
void init()
{
int i, j, k;
repp(i, 1, n)
{
repp(j, i, n)
{
if (i == j)
{
dis[i][j] = 0;
}
else
{
dis[i][j] = dis[j][i] = INF;
}
res[i][j] = res[j][i] = 0;
}
}
}
void prim()
{
int i, j, k, st;
int top = 0;
for (i = 1; i <= n; i++)
{
mini[i] = INF;
vis[i] = 0;
mini[i] = dis[1][i];
}
st = 1;
vis[1] = 1;
mini[1] = 0;
for (i = 1; i <= n - 1; i++)
{
for (j = 1; j <= n; j++)
{
if (vis[j] == 0 && dis[st][j] < mini[j])
{
mini[j] = dis[j][st];
pre[j] = st;
}
}
int min_all = INF;
for (j = 1; j <= n; j++)
{
if (vis[j] == 0 && mini[j] < min_all)
{
min_all = mini[j];
st = j;
}
}
for (j = 1; j <= n; j++)
{
if (vis[j])
{
res[j][st] = res[st][j] = max(res[j][pre[st]], mini[st]);
}
}
vis[st] = 1;
}
}
void solve()
{
int i, j, k;
int u, v, w;
repp(i, 1, m)
{
sa(edge[i].u), sa(edge[i].v), sa(edge[i].w);
dis[edge[i].u][edge[i].v] = dis[edge[i].v][edge[i].u] = min(dis[edge[i].v][edge[i].u], edge[i].w);
}
prim();
repp(i, 1, q)
{
sa(u), sa(w);
if (res[edge[u].u][edge[u].v] >= w)
{
puts("Yes");
}
else
{
puts("No");
}
}
}
int main()
{
while (scanf("%d%d%d", &n, &m, &q) != EOF)
{
init();
solve();
}
return 0;
}
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 1415 | Accepted: 527 | |
Case Time Limit: 2000MS |
“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
题意是询问一个边变小之后能不能进入到最小生成树的边中。
求最小生成树,计算两个点在最小生成树中的最大边,如果减过之后的边比这个最大边都小,那就可以替换到最小生成树中。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x3fffffff
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
const int maxn = 1e3 + 5;
const ll mod = 1000000;
const double PI = acos(-1.0);
int n, m, q;
int mini[maxn], vis[maxn], pre[maxn];
int dis[maxn][maxn], res[maxn][maxn];
struct ed
{
int u;
int v;
int w;
}edge[200005];
void init()
{
int i, j, k;
repp(i, 1, n)
{
repp(j, i, n)
{
if (i == j)
{
dis[i][j] = 0;
}
else
{
dis[i][j] = dis[j][i] = INF;
}
res[i][j] = res[j][i] = 0;
}
}
}
void prim()
{
int i, j, k, st;
int top = 0;
for (i = 1; i <= n; i++)
{
mini[i] = INF;
vis[i] = 0;
mini[i] = dis[1][i];
}
st = 1;
vis[1] = 1;
mini[1] = 0;
for (i = 1; i <= n - 1; i++)
{
for (j = 1; j <= n; j++)
{
if (vis[j] == 0 && dis[st][j] < mini[j])
{
mini[j] = dis[j][st];
pre[j] = st;
}
}
int min_all = INF;
for (j = 1; j <= n; j++)
{
if (vis[j] == 0 && mini[j] < min_all)
{
min_all = mini[j];
st = j;
}
}
for (j = 1; j <= n; j++)
{
if (vis[j])
{
res[j][st] = res[st][j] = max(res[j][pre[st]], mini[st]);
}
}
vis[st] = 1;
}
}
void solve()
{
int i, j, k;
int u, v, w;
repp(i, 1, m)
{
sa(edge[i].u), sa(edge[i].v), sa(edge[i].w);
dis[edge[i].u][edge[i].v] = dis[edge[i].v][edge[i].u] = min(dis[edge[i].v][edge[i].u], edge[i].w);
}
prim();
repp(i, 1, q)
{
sa(u), sa(w);
if (res[edge[u].u][edge[u].v] >= w)
{
puts("Yes");
}
else
{
puts("No");
}
}
}
int main()
{
while (scanf("%d%d%d", &n, &m, &q) != EOF)
{
init();
solve();
}
return 0;
}
相关文章推荐
- ios之多线程-NSThead、GCD、NSOperationQueue
- iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用
- hdu 5667 Sequence 矩阵快速幂
- Golang通过http.NewRequest实现模拟请求
- SDUT Mountain Subsequences 2607 dp(字母表优化) 2013年山东acm省赛
- CoreThink主题开发(四)使用H-ui开发博客主题之首页幻灯片轮播
- CoreThink主题开发(三)使用H-ui开发博客主题之定义整站样式
- js构建ui的统一异常处理方案(四)
- Quick Sort & Binary Merge Sort
- js构建ui的统一异常处理方案(四)
- hdu 5412 CRB and Queries(动态求区间第k小+整体二分)
- CoreThink主题开发(二)使用H-ui开发博客主题之把H-ui放入你的主题目录中
- HDU1242-Rescue
- Django build_in 登录注册模块 注销时页面是admin模块自带的注销页面的原因
- SPOJ GSS1 Can you answer these queries I
- JAVA GUI的介绍
- the-type-java-lang-charsequence-cannot-be-resolved-in-package-declaration
- VUE 入门笔记
- UITableViewCell图片视差效果
- hdu1297Children’s Queue (递推+大数)