• 最短路径变形(最大值的最小值)


    http://poj.org/problem?id=1797

    题意:n个城市,m条双向边每条边有个承受重量权值,问1到n最大承重量。

    解法:初始化为0,选权值大的边,更新:如果dis[j] < min(dis[pos] , ma[pos][j]) 这样更新使得到达j的承重量尽可能的大。

    //#include<bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #include <stdlib.h>
    using namespace std;
    typedef long long ll ;
    #define int ll
    #define mod 100
    #define gcd(m,n) __gcd(m, n)
    #define rep(i , j , n) for(int i = j ; i <= n ; i++)
    #define red(i , n , j)  for(int i = n ; i >= j ; i--)
    #define ME(x , y) memset(x , y , sizeof(x))
    //int lcm(int a , int b){return a*b/gcd(a,b);}
    //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
    //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
    //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
    #define INF  0x3f3f3f3f
    #define PI acos(-1)
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define pb push_back
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define size(v) (int)(v.size())
    #define cin(x) scanf("%lld" , &x);
    const int N = 1e5+9;
    const int maxn = 1e3+9;
    const double esp = 1e-6;
    int ma[maxn][maxn] , vis[maxn] , dis[maxn];
    int n , m , cnt;
    
    void dijkstra(int u){
        rep(i , 1 , n){
            dis[i] = ma[u][i];
        }
        vis[u] = 1 ;
        rep(i , 1 , n-1){
            int pos ;
            int maxx = -INF;
            rep(j , 1 , n){
                if(!vis[j] && maxx < dis[j]){
                    pos = j ;
                    maxx = dis[j];
                }
            }
            vis[pos] = 1 ;
            rep(j , 1 , n){
                if(!vis[j] && min(dis[pos] , ma[pos][j]) > dis[j]){
                    dis[j] = min(dis[pos] , ma[pos][j]);
                }
            }
        }
    }
    
    void init(){
        ME(ma , 0);
        ME(vis , 0);
    }
    void solve(){
        init();
        scanf("%lld%lld" , &n , &m);
        rep(i , 1 , m){
            int u , v , w ;
            scanf("%lld%lld%lld" , &u , &v , &w);
            ma[u][v] = ma[v][u] = max(ma[v][u] , w);
        }
        dijkstra(1);
        cout << "Scenario #" << ++cnt << ":" << endl << dis[n] << endl << endl;
    }
    
    signed main()
    {
        init();
        int t ;
        cin >> t ;
        while(t--){
            solve();
        }
    }
    

     http://poj.org/problem?id=2253

    题意:一只青蛙要跳到另一只青蛙的所在石头,给出n个石头,从1到n,使跳跃的所需最小距离范围最小为多少?

    解法:更新:如果dis[j] > max(dis[pos] , pos[j]) , 使得到达dis【j】的最长距离最小

    //#include<bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #include <stdlib.h>
    using namespace std;
    typedef long long ll ;
    #define int ll
    #define mod 1000000007
    #define gcd(m,n) __gcd(m, n)
    #define rep(i , j , n) for(int i = j ; i <= n ; i++)
    #define red(i , n , j)  for(int i = n ; i >= j ; i--)
    #define ME(x , y) memset(x , y , sizeof(x))
    int lcm(int a , int b){return a*b/gcd(a,b);}
    //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
    //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
    //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
    #define INF  0x3f3f3f3f
    #define PI acos(-1)
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define pb push_back
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define size(v) (int)(v.size())
    #define cin(x) scanf("%lld" , &x);
    const int N = 1e7+9;
    const int maxn = 2e2+9;
    const double esp = 1e-6;
    double ma[maxn][maxn];
    double dis[maxn];
    int vis[maxn], n , m, cnt;
    pii a[maxn];
    
    void dijia(int r){
        rep(i , 1 , n){
            dis[i] = ma[r][i];
        }
        vis[r] = 1 ;
        rep(i , 1 , n-1){
            int pos ;
            double mi = INF;
            rep(j , 1 , n){
                if(!vis[j] && mi > dis[j]){
                    pos = j ;
                    mi = dis[j];
                }
            }
            vis[pos] = 1 ;
            rep(j , 1 , n){
                double x = max(dis[pos] , ma[pos][j]);
                if(!vis[j] && x < dis[j]){
                    dis[j] = x ;
                }
            }
        }
    }
    void init(){
        fill(ma[0] , ma[0]+maxn*maxn , INF);
        ME(vis,0);
    }
    void solve(){
        init();
        int bx , by , ex , ey;
        scanf("%lld%lld%lld%lld" , &bx , &by , &ex , &ey);
        a[1].fi = bx , a[1].se = by , a[n].fi = ex , a[n].se = ey;
        rep(i , 2 , n-1){
            int u , v ;
            scanf("%lld%lld" , &a[i].fi , &a[i].se);
        }
        rep(i , 1 , n){
            rep(j , 1 , i-1){
                ma[i][j] = ma[j][i] = min(ma[i][j] , sqrt((a[i].fi - a[j].fi)*(a[i].fi - a[j].fi) + (a[i].se - a[j].se) * (a[i].se - a[j].se)));
            }
        }
        dijia(1);
        cout << "Scenario #" << ++cnt << endl;
        printf("Frog Distance = %.3f
    
    " , dis[n]);
    }
    
    signed main()
    {
        //int t ;
        //cin >> t ;
        //while(t--)
        while(~scanf("%lld" , &n) && n)
            solve();
    }
    
  • 相关阅读:
    使用logstash收集java、nginx、系统等常见日志
    day71-Auth模块,BBS小作业初始
    day70-django中间件
    day69-form源码,cookies与session
    day68-分布器的使用,forms组件
    day67-ajax发送数据,分页器等
    day66-图书管理系统,Ajax,choices参数等
    复习知识点-没搞清楚的总结
    day65-聚合查询,分组查询,F与Q查询,事务,参数
    day64-表单查询,双下查询,各种查询(model层,数据库操作)
  • 原文地址:https://www.cnblogs.com/nonames/p/12543858.html
Copyright © 2020-2023  润新知