2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学) Meeting(SPAF+拆点)

本文探讨了在大规模数据集上使用SPFA算法进行路径寻优的问题,通过优化建边方式减少时间消耗,并解决了农场中两个角色如何快速会面的具体案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Meeting

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


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   m .   2n105 . 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

123

【思路】

两遍SPFA  一个从1 一个从n 开始

问题在于 数据边太大,   一般暴力 肯定会超时,  因此 在 建边上优化,   对于每个集合 建立 一个 出口点,   每次扫 这个出口点,   n个集合 就是n个点

这样大大优化建边


【注意】

可能会爆 int  用long long

【代码】

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b



#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt", What a Ridiculous Election"w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r

using namespace std;
typedef long long ll;

const ll  INF=1e18+10;
const int maxn=2e6+5;
const int MOD=1000000007;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
int n,m;
ll dis[maxn],dis1[maxn];
int head[maxn];
int ans[maxn],tol;
ll cont;
int vis[maxn];
struct Edge{
    int v,w,next;
}edge[maxn];
void init()
{
    cont=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    edge[++cont].v=v;
    edge[cont].w=w;
    edge[cont].next=head[u];
    head[u]=cont;
}

void SPFA(int st)
{
    queue<int> Q;
    for(int i=0;i<=n+m+3;i++)
        dis[i]=INF;
    mem(vis,0);
    dis[st]=0;
    Q.push(st);
    vis[st]=1;
    while(!Q.empty())
    {
        ll u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int t=head[u];t!=-1;t=edge[t].next)
        {
            ll v=edge[t].v;
            ll w=edge[t].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
}
int main()
{
    int T,cot=0;
    cin>>T;
    int te,num,x;
    while(T--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&te,&num);
            for(int j=1;j<=num;j++)
            {
                scanf("%d",&x);
                add(x,i+n,te);
                add(i+n,x,te);
            }
        }
        SPFA(1);
        printf("Case #%d: ",++cot);
        if(dis[n]==INF)
        {
            printf("Evil John\n");
            continue;
        }
        for(int i=0;i<=maxn;i++)
            dis1[i]=dis[i];
        SPFA(n);
        tol=0;
        ll res=INF;

        for(int i=1;i<=n;i++)
        {
            res=min(res,max(dis[i],dis1[i]));
        }
        printf("%lld\n",res/2);
        tol=1;
        for(int i=1;i<=n;i++)
        {
            if(max(dis[i],dis1[i])==res)
                ans[tol++]=i;
        }
        for(int i=1;i<tol;i++)
        {
            if(i==1)
                printf("%d",ans[i]);
            else
                printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值