HDU 2377 && ZOJ 2412

~~~~

输入好长。。。

思路就是对线路上的每一个点BFS记录各个点到其所需的star值,然后ans记录所需的最大star值,最后输出ans最小的star值及相应的id。

开始建链接矩阵,结果MLE,于是修改了下。

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2377

http://acm.zju.edu.cn/onlinejudge/showProblemStatus.do?problemId=1912

~~~~

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define N 10000
#define INF 0x7fffffff
using namespace std;

int nz,nr;
int z[N];   //记录每一个zone周围的相连的zone的数目
int g[N][15];   //与其相连的zone的id
int res[N];     //每一次bfs后距离起点所需的star值
int ans[N];     //纪律每一个区域所需的最大star值
int vis[N];
void bfs(int u)
{
    queue<int> q;
    q.push(u);
    res[u]=1;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=0;i<z[u];i++)
        {
            if(!vis[g[u][i]])
            {
                vis[g[u][i]]=1;
                int v=g[u][i];
                res[v]=res[u]+1;
                q.push(v);
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&nz,&nr);
        for(int i=0;i<nz;i++)
        {
            int m,id;
            scanf("%d%d",&id,&m);
            z[id]=m;
            for(int j=0;j<m;j++)
            {
                int t;
                scanf("%d",&t);
                g[id][j]=t;
            }
        }
        memset(res,0,sizeof(res));
        memset(ans,0,sizeof(ans));
        for(int i=0;i<nr;i++)
        {
            int m;
            scanf("%d",&m);
            for(int j=0;j<m;j++)
            {
                int s;
                scanf("%d",&s);
                memset(vis,0,sizeof(vis));
                vis[s]=1;
                bfs(s);
                for(int k=0;k<10000;k++)
                    ans[k]=max(ans[k],res[k]);
            }
        }
        int star=INF,center;
        for(int i=0;i<10000;i++)
        {
            if(ans[i] && ans[i]<star)
            {
                star=ans[i];
                center=i;
            }
        }
        printf("%d %d\n",star,center);
    }
    return 0;
}

HDU 2377 && ZOJ 2412

时间: 2024-11-05 06:30:21

HDU 2377 && ZOJ 2412的相关文章

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages

HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

POJ1607 &amp; HDU 1330 &amp; ZOJ 1216 Deck(数学题)

题目链接: POJ  1607 : http://poj.org/problem?id=1607 HDU 1330 :http://acm.hdu.edu.cn/showproblem.php?pid=1330 ZOJ  1216 : Description A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table

HDU 1242 &amp;&amp; ZOJ 1649( BFS (队列 || 优先队列)).

~~~~ 突然发现一篇搜索的题目都有写.昨天发现道bfs题目,HDU上AC, ZOJ上WA.不得不说HDU上的数据之水.. 今天早起刷题有了思路,并用队列和单调队列都写了一遍,0MS飘过~~ ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 ~~~~ 首先有坑的地方是friends,对嘛,朋友有很多,ang

hdu 2377 Bus Pass

Bus Pass Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 667    Accepted Submission(s): 271 Problem Description You travel a lot by bus and the costs of all the seperate tickets are starting to

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

HDU 1988 &amp; ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1988 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1990 Problem Description The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As

HDU 1990 &amp; ZOJ 2992 Monkey Vines(数学啊)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1990 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1991 Problem Description Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and junipe