zoukankan      html  css  js  c++  java
  • HDOJ5521(巧妙构建完全图)

    Meeting

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


    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
     
    思路:任意取一个block举例建图。设block中有n个节点,,有题意可知,block是一个无向完全图。若按传统的方法建图,则无向完全图中有n个节点, n*(n-1)条边。我们换一种方式建图,在block中增加两个节点u,v。再加入一条有向边(u,v),然后将block中的所有节点xi,加入两条有向边(xi,u)、(v,xi)。最终得到的是一幅有n+2个节点2*n+1个有向边的有向图,其等价于按传统方法构建的完全图。
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int MAXN = 400005;
    const int INF = 0x3f3f3f3f;
    struct Edge{
        int to, w, net;
    }es[4000005];
    int head[MAXN], tot;
    int n, m;
    void addedge(int u, int v, int w)
    {
        es[tot].to = v;
        es[tot].w = w;
        es[tot].net = head[u];
        head[u] = tot++;
    }
    void spfa(int src, int d[], bool vis[], int n)
    {
        for(int i = 1; i <= n; i++)
        {
            d[i] = INF;
            vis[i] =false;
        }
        d[src] = 0;
        queue<int> que;
        que.push(src);
        while(!que.empty())
        {
            int u = que.front(); que.pop();
            vis[u] = false;
            for(int i = head[u]; i != -1; i = es[i].net)
            {
                Edge e = es[i];
                if(d[e.to] > d[u] + e.w)
                {
                    d[e.to] = d[u] + e.w;
                    if(!vis[e.to])
                    {
                        vis[e.to] = true;
                        que.push(e.to);
                    }
                }
            }
        }
    }
    int d[2][MAXN];
    bool vis[MAXN];
    int vec[MAXN], len;
    int main()
    {
        int T;
        scanf("%d", &T);
        for(int cas = 1; cas <= T; cas++)
        {
            memset(head, -1, sizeof(head));
            tot = 0;
            scanf("%d %d", &n ,&m);
            int newN = n;
            for(int i = 0; i < m; i++)
            {
                int w, s;
                scanf("%d %d", &w, &s);
                //巧妙构图
                int u = ++newN;
                int v = ++newN;
                addedge(u, v, w);
                for(int j = 0; j < s; j++)
                {
                    int x;
                    scanf("%d", &x);
                    addedge(x, u, 0);
                    addedge(v, x, 0);
                }
            }
    
            spfa(1, d[0], vis, newN);
            spfa(n, d[1], vis, newN);
    
            int mn = INF;
            for(int i = 1; i <= n; i++)
            {
                int mx = max(d[0][i], d[1][i]);
                if(mn > mx)
                {
                    mn = mx;
                }
            }
            if(mn == INF)
            {
                printf("Case #%d: Evil John
    ", cas);
                continue;
            }
            len = 0;
            for(int i = 1; i <= n; i++)
            {
                if(max(d[0][i], d[1][i]) == mn)
                {
                    vec[len++] = i;
                }
            }
            printf("Case #%d: %d
    ", cas, mn);
            for(int i = 0; i < len -1; i++)
            {
                printf("%d ", vec[i]);
            }
            printf("%d
    ", vec[len-1]);
        }
        return 0;
    }
  • 相关阅读:
    QtDBus编程详解
    QProcess详解
    python 爬虫 亚航 指定日期间的航线
    python 模块
    centos postgres 安装、远程连接
    python 爬虫 anyproxy
    python_scrapy_filespipe重写
    python_xpath
    常见问题汇总
    python_scrapy_log日志
  • 原文地址:https://www.cnblogs.com/program-ccc/p/6020281.html
Copyright © 2011-2022 走看看