zoukankan      html  css  js  c++  java
  • BZOJ1208|HNOI旅行comf|并查集

    Description
    给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出这个比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。
    Input
    第一行包含两个正整数,N和M。 下来的M行每行包含三个正整数:x,y和v。表示景点x到景点y之间有一条双向公路,车辆必须以速度v在该公路上行驶。 最后一行包含两个正整数s,t,表示想知道从景点s到景点t最大最小速度比最小的路径。s和t不可能相同。
    Output
    如果景点s到景点t没有路径,输出“IMPOSSIBLE”。否则输出一个数,表示最小的速度比。如果需要,输出一个既约分数。
    Sample Input
    【样例输入1】
    4 2
    1 2 1
    3 4 2
    1 4【样例输入2】
    3 3
    1 2 10
    1 2 5
    2 3 8
    1 3
    【样例输入3】
    3 2
    1 2 2
    2 3 4
    1 3
    Sample Output
    【样例输出1】
    IMPOSSIBLE【样例输出2】
    5/4
    【样例输出3】
    2
    【数据范围】
    1< N < = 500
    1 < = x, y < = N,0 < v < 30000,x ≠ y
    0 < M < =5000
    分析:和舒适的路线一样。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    struct node{
           int x,y,v;
    }e[5001];
    
    int fa[501];
    
    bool cmp(node a,node b) { return a.v<b.v; }
    
    int gcd(int a,int b) { return b?gcd(b,a%b):a; }
    
    int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
    
    int main()
    {
        int n,m;
        cin >> n >> m;
        for (int i=1; i<=m; i++) cin >> e[i].x >> e[i].y >> e[i].v;
        sort(e+1,e+m+1,cmp);
        int st=1,mx,mn;
        int s,t,ansmx=1,ansmn=0;
        cin >> s >> t;
        while (st<m)
        {
              int k;
              for (int i=1; i<=n; i++) fa[i]=i;
              for (int i=st; i<=m; i++)
              {
                  fa[e[i].x]=find(e[i].y);
                  if (find(s)==find(t)) { mx=e[i].v; k=i; break; }
              }
              if (mx==-1) if (!ansmn) { cout<<"IMPOSSIBLE"; return 0; }
                           else break;
              for (int i=1; i<=n; i++) fa[i]=i;
              for (;k>=1; k--) 
              {
                  fa[e[k].x]=find(e[k].y);
                  if (find(s)==find(t)) { mn=e[k].v; st=k+1; break; }
              }
              if(mn==-1) if (!ansmn) { cout<<"IMPOSSIBLE"; return 0; }
                          else break;
             int r=gcd(mx,mn); mx/=r; mn/=r;
             if (ansmx*mn>ansmn*mx) {ansmn=mn;ansmx=mx;}
        }
        if (ansmn==1) cout << ansmx;
             else cout << ansmx << "/" << ansmn << endl;
        return 0;
    }
  • 相关阅读:
    How to function call using 'this' inside forEach loop
    jquery.validate.unobtrusive not working with dynamic injected elements
    Difference between jQuery.extend and jQuery.fn.extend?
    Methods, Computed, and Watchers in Vue.js
    Caution using watchers for objects in Vue
    How to Watch Deep Data Structures in Vue (Arrays and Objects)
    Page: DOMContentLoaded, load, beforeunload, unload
    linux bridge
    linux bridge
    EVE-NG网卡桥接
  • 原文地址:https://www.cnblogs.com/Shymuel/p/4656295.html
Copyright © 2011-2022 走看看