Hdu2377bus pass (more complex graph creation + spfa)

Source: Internet
Author: User
Question link: Ah, haha, click me
Train of Thought: the question is to give a lot of stations, and then you need to find a community with the minimum maximum distance from these stations .. Therefore, if spfa is performed for each station, the maximum value is obtained for each community, and the maximum and minimum value is obtained after scanning for each community .. There are also questions that require the smallest ID, so the order is arranged. Question: 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 descriptionyou travel a lot by bus and the costs of all the Seperate tickets are starting to add up.

Therefore you want to see if it might be advantageous for you to buy a bus pass.

The way the bus system works in your country (and also in the Netherlands) is as follows:

When you buy a bus pass, you have to indicate a center zone and a star value. you are allowed to travel freely in any zone which has a distance to your center zone which is less than your star value. for example, if you have a star value of one, you can only travel in your center zone. if you have a star value of two, you can also travel in all adjacent zones, et Cees.

You have a list of all bus trips you frequently make, and wowould like to determine the minimum star value you need to make all these trips using your buss pass. but this is not always an easy task. for example look at the following figure:



Here you want to be able to travel from A to B and from B to D. the best center zone is 7400, for which you only need a star value of 4. note that you do not even visit this zone on your trips!
 
Inputon the first line an integert (1 <= T <= 100): the number of test cases. Then for each test case:

One line with two integersnz (2 <= NZ <= 9 999) andnr (1 <= nR <= 10): The number of zones and the number of bus trips, respectively.

NZ lines starting with two integers Idi (1 <= Idi <= 9 999) and MZI (1 <= MZI <= 10 ), A number identifying the I-th zone and the number of zones adjacent to it, followed by MZI integers: the numbers of the adjacent zones.

NR lines starting with one integer MRI (1 <= MRI <= 20), indicating the number of zones the ith bus trip visits, followed by MRI integers: the numbers of the zones through which the bus passes in the order in which they are visited.

All zones are connected, either directly or via other zones.

 
Outputfor each test case:

One line with two integers, the minimum star value and the ID of a center zone which achieves this minimum star value. If there are multiple possibilities, choose the zone with the lowest number.

 
Sample Input
117 27400 6 7401 7402 7403 7404 7405 74067401 6 7412 7402 7400 7406 7410 74117402 5 7412 7403 7400 7401 74117403 6 7413 7414 7404 7400 7402 74127404 5 7403 7414 7415 7405 74007405 6 7404 7415 7407 7408 7406 74007406 7 7400 7405 7407 7408 7409 7410 74017407 4 7408 7406 7405 74157408 4 7409 7406 7405 74077409 3 7410 7406 74087410 4 7411 7401 7406 74097411 5 7416 7412 7402 7401 74107412 6 7416 7411 7401 7402 7403 74137413 3 7412 7403 74147414 3 7413 7403 74047415 3 7404 7405 74077416 2 7411 74125 7409 7408 7407 7405 74156 7415 7404 7414 7413 7412 7416
 
Sample output
4 7400
 
Sourcebapc2007
Recommendlcy | we have carefully selected several similar problems for you: 2722 2363 2482 2962
Code:
#include<cstdio>#include<queue>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#define INF 0x3f3f3f3fusing namespace std;const int maxn=9999+10;int dis[maxn],id[maxn],max_dis[maxn];bool vis[maxn];vector<int>vec[maxn];int t,n,bus,cal;bool cmp(int a,int b){    return a<b;}void spfa(int st){    queue<int>Q;    while(!Q.empty())  Q.pop();    memset(vis,false,sizeof(vis));    memset(dis,INF,sizeof(dis));    Q.push(st);    dis[st]=0;    vis[st]=true;    while(!Q.empty())    {        int temp=Q.front();        Q.pop();        vis[temp]=false;        for(int i=0;i<vec[temp].size();i++)        {            if(dis[temp]+1<dis[vec[temp][i]])            {                dis[vec[temp][i]]=dis[temp]+1;                if(!vis[vec[temp][i]])                {                  vis[vec[temp][i]]=true;                  Q.push(vec[temp][i]);                }            }        }    }}void read_graph(){    memset(max_dis,0,sizeof(max_dis));    int u;    scanf("%d%d",&n,&bus);    for(int i=1;i<=maxn;i++)        vec[i].clear();    for(int i=1;i<=n;i++)    {        scanf("%d%d",&id[i],&cal);        for(int j=1;j<=cal;j++)        {            scanf("%d",&u);            vec[id[i]].push_back(u);        }    }    while(bus--)    {        scanf("%d",&cal);        while(cal--)        {            int st;            scanf("%d",&st);            spfa(st);            for(int i=1;i<=n;i++)            {                if(dis[id[i]]>max_dis[id[i]])                    max_dis[id[i]]=dis[id[i]];            }        }    }    int ans=999999999,ans_id;    sort(id+1,id+1+n,cmp);    for(int i=1;i<=n;i++)    {        if(max_dis[id[i]]<ans)        {            ans=max_dis[id[i]];            ans_id=id[i];        }    }    printf("%d %d\n",ans+1,ans_id);}int main(){    scanf("%d",&t);    while(t--)    {        read_graph();    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.