BZOJ 1082 [二分][广搜]

3 篇文章 0 订阅
2 篇文章 0 订阅

1083: [SCOI2005]繁忙的都市
Description
  城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连接。这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了。每条道路都有一个分值,分值越小表示这个道路越繁忙,越需要进行改造。但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的要求: 1. 改造的那些道路能够把所有的交叉路口直接或间接的连通起来。 2. 在满足要求1的情况下,改造的道路尽量少。 3. 在满足要求1、2的情况下,改造的那些道路中分值最大的道路分值尽量小。任务:作为市规划局的你,应当作出最佳的决策,选择那些道路应当被修建。
Input
  第一行有两个整数n,m表示城市有n个交叉路口,m条道路。接下来m行是对每条道路的描述,u, v, c表示交叉路口u和v之间有道路相连,分值为c。(1≤n≤300,1≤c≤10000)
Output
  两个整数s, max,表示你选出了几条道路,分值最大的那条道路的分值是多少。
Sample Input
4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8
Sample Output
3 6

当看到“分值最大的道路分值尽量小”时就应该想到二分查找。
第一问很显然是n - 1,第二问只要二分查找答案 x 每次检验能否通过所有小于等于 x 的边使整个图联通(其实可以用并查集的,但我没改,所以可能慢一些)

附代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#define N 305
using namespace std;

struct edge {
  int to, key, next;
  edge(void) {}
  edge(int t, int k, int n):to(t), key(k), next(n) {}
}G[N * N * 2];
bool vis[N];
int cnt = 0, n, m, x, y, z;
int head[N], Q[N];

inline char get(void) {
  static char buf[100000], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
    if (p1 == p2) return EOF;
  }
  return *p1++;
}
inline void read(int &x) {
  x = 0; char c = get();
  for (; c < '0' || c > '9'; c = get());
  for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = get());
}
inline void add_edge(int from, int to, int key) {
  G[cnt] = edge(to, key, head[from]);
  head[from] = cnt++;
  G[cnt] = edge(from, key, head[to]);
  head[to] = cnt++;
}
inline bool check(int limit) {
  static int all, h, r;
  all = n; h = r = 0;
  memset(vis, 0, sizeof(vis));
  Q[r++] = 1; vis[1] = 1;
  while (h < r && all) {
    all--; int x = Q[h++];
    for (int i = head[x]; i != -1; i = G[i].next) {
      edge &e = G[i];
      if (e.key <= limit && !vis[e.to]) {
        vis[e.to] = 1; Q[r++] = e.to;
      }
    }
  }
  return all == 0;
}

int main(void) {
  read(n); read(m);
  memset(head, -1, sizeof(head));
  for (int i = 0; i < m; i++) {
    read(x); read(y); read(z);
    add_edge(x, y, z);
  }
  int left = 1, right = 10000, mid;
  while (left <= right) {
    mid = (left + right) >> 1;
    if (check(mid)) right = mid - 1;
    else left = mid + 1;
  }
  cout << n - 1 << ' ' << right + 1 << endl;
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值