zoukankan      html  css  js  c++  java
  • HDU 5521.Meeting 最短路模板题

    Meeting

    Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 3361    Accepted Submission(s): 1073


    Problem Description
    Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his
    fences they were separated into different blocks. John's farm are divided into n blocks labelled from 1 to n.
    Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
    which shows that it takes they ti minutes to travel from a block in Ei to another block
    in Ei where Ei (1im) is a set of blocks. They want to know how soon they can meet each other
    and which block should be chosen to have the meeting.
     
    Input
    The first line contains an integer T (1T6), the number of test cases. Then T test cases
    follow.

    The first line of input contains n and m2n105. The following m lines describe the sets Ei (1im). Each line will contain two integers ti(1ti109)and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that mi=1Si106.
     
    Output
    For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.

    Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
    The second line contains the numbers of blocks where they meet. If there are multiple
    optional blocks, output all of them in ascending order.
     
    Sample Input
    2
    5 4
    1 3 1 2 3
    2 2 3 4
    10 2 1 5
    3 3 3 4 5
    3 1
    1 2 1 2
     
    Sample Output
    Case #1: 3
    3 4
    Case #2: Evil John
    Hint
    In the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet.
     
    Source
     
    题意:有m个集合,每个集合里面的任意两点均有一条距离为ei的无向边,求1和n到其他点的最短距离中最大值的最小值。
    思路:最短路模板题。每个集合作为作为一个点,对应的点到集合的距离为ei,最后答案/2。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<bitset>
    #include<queue>
    #include<stack>
    #include<map>
    #include<vector>
    using namespace std;
    #define eps 0.0000001
    typedef long long ll;
    typedef pair<int,int> P;
    const int maxn=2e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e18+7;
    struct edge
    {
        int from,to;
        ll w;
    };
    vector<edge>G[maxn];
    priority_queue<P,vector<P>,greater<P> >q;
    ll dist[2][maxn];
    void addedge(int u,int v,ll w)
    {
        G[u].push_back((edge)
        {
            u,v,w
        });
        G[v].push_back((edge)
        {
            v,u,w
        });
    }
    void dij(int t,int s)
    {
        dist[t][s]=0LL;
        q.push(P(dist[t][s],s));
        while(!q.empty())
        {
            P p=q.top();
            q.pop();
            int u=p.second;
            for(int i=0; i<G[u].size(); i++)
            {
                edge e=G[u][i];
                if(dist[t][e.to]>dist[t][u]+e.w)
                {
                    dist[t][e.to]=dist[t][u]+e.w;
                    q.push(P(dist[t][e.to],e.to));
                }
            }
        }
    }
    void init(int n)
    {
        for(int i=0; i<=2*n+10; i++) G[i].clear();
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int Case=1; Case<=T; Case++)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            for(int i=1; i<=m; i++)
            {
                int val;
                scanf("%lld",&val);
                int t;
                scanf("%d",&t);
                while(t--)
                {
                    int s;
                    scanf("%d",&s);
                    addedge(s,n+i,val);
                }
            }
            for(int i=0; i<=2*n+10; i++) dist[0][i]=dist[1][i]=INF;
            dij(0,1);
            dij(1,n);
            ll ans=INF;
            for(int i=1; i<=n; i++)
            {
                //printf("%lld %lld
    ",dist[0][i],dist[1][i]);
                ans=min(ans,max(dist[0][i],dist[1][i]));
            }
            printf("Case #%d: ",Case);
            if(ans>=INF) puts("Evil John");
            else
            {
                printf("%lld
    ",ans/2);
                int cou=0;
                for(int i=1; i<=n; i++)
                {
                    if(!cou&&max(dist[0][i],dist[1][i])==ans) printf("%d",i),cou++;
                    else if(cou&&max(dist[0][i],dist[1][i])==ans) printf(" %d",i),cou++;
                }
                printf("
    ");
            }
            init(n);
        }
        return 0;
    }
    最短路模板题
  • 相关阅读:
    十.总结drf视图
    一.9.多云管理同步服务器
    一.vue 初识
    一.8.django权限管理/drf权限管理
    一.7.服务器之分页和搜索应用
    一.6.序列化应用之服务器同步功能
    一.5.序列化应用之服务器制造厂与型号app功能
    【前端背景UI】鼠标磁性动态蜘蛛网背景源码
    【vue】导入式,使用vue与element UI, 单人开发项目,告别脚手架
    【python后台admin】所有属性显示设置
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7476156.html
Copyright © 2011-2022 走看看