HDU 2377 && ZOJ 2412

來源:互聯網
上載者:User

標籤:bus pass   hdu 2377   zoj 2913   

~~~~

輸入好長。。。

思路就是對線路上的每一個點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 0x7fffffffusing namespace std;int nz,nr;int z[N];   //記錄每一個zone周圍的相連的zone的數目int g[N][15];   //與其相連的zone的idint 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.