Routing

来源:互联网 发布:java高并发重复提交 编辑:程序博客网 时间:2024/05/16 08:36

1072. Routing

Time limit: 1.0 second
Memory limit: 64 MB
There is a TCP/IP net of several computers. It means that:
  1. Each computer has one or more net interfaces.
  2. Each interface is identified by its IP-address and a subnet mask — these are two four-byte numbers with a point after each byte. A subnet mask has a binary representation as follows: there are k 1-bits, then — m 0-bits, k+m=8*4=32 (e.g., 212.220.35.77 — is an IP-address and 255.255.255.128 — is a subnet mask).
  3. Two computers belong to the same subnet, if and only if (IP1 AND NetMask1) = (IP2 AND NetMask2), where IPi and NetMask— are an IP-address and subnet mask of i-th computer, AND — is bitwise.
  4. A packet is transmitted between two computers of one subnet directly.
  5. If two computers belong to different subnets, a packet is to be transmitted via some other computers. The packet can pass from one subnet to another only on computer that has both subnets interfaces.
Your task is to find the shortest way of a packet between two given computers.

Input

The first line contains a number N — an amount of computers in the net, then go N sections, describing interfaces of each computer. There is a number K in the first line of a section — that is an amount of interfaces of the computer, then go K lines — descriptions of the interfaces, i.e. its IP-address and a subnet mask. The last line of an input contains two integers — the numbers of the computers that you are to find a way between them.
You may assume that 2 ≤ N ≤ 90 and K ≤ 5.

Output

The word “Yes” if the route exists, then in the next line the computer numbers passed by the packet, separated with a space. The word “No” otherwise.

Sample

inputoutput
6210.0.0.1 255.0.0.0192.168.0.1 255.255.255.0110.0.0.2 255.0.0.03192.168.0.2 255.255.255.0212.220.31.1 255.255.255.0212.220.35.1 255.255.255.01212.220.31.2 255.255.255.02212.220.35.2 255.255.255.0195.38.54.65 255.255.255.2241 195.38.54.94 255.255.255.2241 6
Yes1 3 5 6

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 99int n;int st,en;int NetS[maxn][5]= {0};int jN[maxn]={0};int head[maxn]= {0};int Map[maxn][maxn]= {0};int Min[maxn]={0};int vis[maxn]={0};int mink=0;void read(){    cin>>n;    memset(head,-1,sizeof(head));    for(int i=1; i<=n; i++)    {        scanf("%d",&jN[i]);        for(int k=0; k<jN[i]; k++)        {            int t1[4],t2[4];            scanf("%d.%d.%d.%d",&t1[0],&t1[1],&t1[2],&t1[3]);/**读入数据**/            scanf("%d.%d.%d.%d",&t2[0],&t2[1],&t2[2],&t2[3]);            for(int p=0; p<4; p++)            {                NetS[i][k]*=1000;                NetS[i][k]+=(t1[p]&t2[p]);/**计算子网掩码并储存**/            }        }    }    cin>>st>>en;    memset(Map,1,sizeof(Map));    memset(Min,1,sizeof(Min));    int temp=Min[0];    for(int j=1; j<=n; j++)/**根据掩码的等价关系,构造无向图**/        for(int k=j; k<=n; k++)        {            if(k==j)            {                Map[j][k]=0;                Map[k][j]=0;                if(j==st)                {                    vis[k]=1;/**设置源点**/                    Min[k]=0;                }            }            else                for(int i1=0; i1<jN[j]; i1++)                    for(int i2=0; i2<jN[k]; i2++)                        if(NetS[j][i1]==NetS[k][i2])                        {                            Map[j][k]=1;                            Map[k][j]=1;                            if(j==st)                            {                                head[k]=st;                                Min[k]=1;                            }                            goto L;                        }L:;            if(j==st)            {                if(Min[k]<temp&&k!=st)                {                    mink=k;                    temp=Min[k];                }            }        }}void Dijkstra()/**单源最短路**/{    head[mink]=st;    for(int j=1;j<=n-1;j++)    {        vis[mink]=1;        int flag=1;        int tempd=Min[0],tempk;        for(int i=1;i<=n;i++)        {            if(i==st||i==mink)                continue;            if(Min[mink]+Map[mink][i]<Min[i])            {                Min[i]=Min[mink]+Map[mink][i];                head[i]=mink;/**记录路径**/            }            if(Min[i]<tempd&&!vis[i])            {                tempd=Min[i];                tempk=i;            }        }        mink=tempk;    }}void DFSprint(){    int path[maxn]={0};    int i=0;    path[i++]=en;    while(en!=st)    {        path[i]=head[en];        en=path[i];/**搜索路径**/        i++;    }    for(i--;i>=1;i--)        cout<<path[i]<<" ";    cout<<path[i];}int main(){    read();    Dijkstra();    if(Min[en]!=Min[0])    {        cout<<"Yes"<<endl;        DFSprint();    }    else        cout<<"No"<<endl;    return 0;}