NC19963 旅行

[HAOI2006]旅行COMF

https://ac.nowcoder.com/acm/problem/19963

旅行

题目地址:

https://ac.nowcoder.com/acm/problem/19963

基本思路:

数据范围比较小,我们考虑暴力枚举到最小速度,并且在每次最小速度确定情况下的找到对应最小的最大速度。
具体的我们只要将边从小到大排序,然后枚举到最小速度对应边后,并查集维护往后连边,直到联通就行了。
得到每次的结果后取最小,注意这里要写个比较函数比较分数。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define debug(x) cerr << #x << " = " << x << '\n';
#define pll pair <ll, ll>
#define fir first
#define sec second
#define INF 0x3f3f3f3f
#define int ll

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int maxn  = 510;
int n,m,s,t;
struct Edge{
    int u,v,w;
    bool operator < (const Edge &no) const{
      return w < no.w;
    }
};
int par[maxn];
int find(int x){return par[x] == x ? x : par[x] = find(par[x]); }
bool cmp(int a1,int a2,int r1,int r2) { return r2 * a1 < r1 * a2; }
signed main() {
  IO;
  cin >> n >> m;
  vector<Edge> edge;
  rep(i,1,m){
    int u,v,w;
    cin >> u >> v >> w;
    edge.push_back({u,v,w});
  }
  cin >> s >> t;
  sort(all(edge));
  int a1 = -1,a2 = -1;
  for(int i = 0 ; i < SZ(edge) ; i++) {
    rep(j, 1, n) par[j] = j;
    int j = i;
    for (; j < SZ(edge); j++) {
      int u = edge[j].u, v = edge[j].v;
      if (find(u) != find(v)) {
        int x = find(u), y = find(v);
        par[x] = y;
      }
      if (find(s) == find(t)) { break; }
    }
    if(find(s) != find(t)) break;
    int r1 = edge[i].w, r2 = edge[j].w;
    if (a1 == -1 && a2 == -1) a1 = r1, a2 = r2;
    else {
      if (cmp(a1, a2, r1, r2)) a1 = r1, a2 = r2;
    }
  }
  if(a1 == -1 && a2 == -1) cout << "IMPOSSIBLE" << '\n';
  else {
    int gcd = __gcd(a1,a2);
    a1 /= gcd,a2 /= gcd;
    if(a1 == 1) cout << a2 << '\n';
    else cout << a2 << '/' << a1 << '\n';
  }
  return 0;
}
全部评论

相关推荐

一名愚蠢的人类:多少games小鬼留下了羡慕的泪水
投递荣耀等公司10个岗位
点赞 评论 收藏
分享
伟大的烤冷面被普调:暨大✌🏻就是强
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务