ural 1072. Routing

时间:2023-03-08 21:41:14

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 NetMaski — 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

input output
6
2
10.0.0.1 255.0.0.0
192.168.0.1 255.255.255.0
1
10.0.0.2 255.0.0.0
3
192.168.0.2 255.255.255.0
212.220.31.1 255.255.255.0
212.220.35.1 255.255.255.0
1
212.220.31.2 255.255.255.0
2
212.220.35.2 255.255.255.0
195.38.54.65 255.255.255.224
1
195.38.54.94 255.255.255.224
1 6
Yes
1 3 5 6
Problem Author: Evgeny Kobzev
Problem Source: Ural State Univerisity Personal Contest Online February'2001 Students Session
Difficulty: 464 
题意:给出n台计算机,每台计算机有若干对IP地址以及子网掩码,当且仅当存在(IP地址i AND 子网掩码i) = (IP地址j AND 子网掩码j)时,两台计算机才可以联系。
问从一台计算机到另一台计算机最短要经过多少计算机。
输出方案。
分析:实际上就是最短路。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n;
int length[N];
vector<unsigned int> feature[N];
bool graph[N][N];
queue<int> que;
int dp[N], from[N], st, ed; inline unsigned int Get()
{
const static unsigned int fact[] = {, , , };
unsigned int ret = ;
for(int i = ; i < ; i++)
{
int x = (unsigned int) Getint();
ret += x * fact[i];
}
return ret;
} inline void Input()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
scanf("%d", &length[i]);
//cout << i << ": ";
for(int j = ; j <= length[i]; j++)
{
unsigned int x, y;
x = Get();
y = Get();
feature[i].pub(x & y);
//cout << (x & y) << ' ';
}
//cout << endl;
}
scanf("%d%d", &st, &ed);
} inline bool find(int x, int y)
{
int len1 = sz(feature[x]), len2 = sz(feature[y]);
for(int i = ; i < len1; i++)
for(int j = ; j < len2; j++)
if(feature[x][i] == feature[y][j])
return ;
return ;
} inline void Solve()
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(i != j && find(i, j))
graph[i][j] = ; for(int i = ; i <= n; i++)
from[i] = , dp[i] = INF;
dp[st] = ;
que.push(st);
while(sz(que))
{
int u = que.front();
que.pop();
for(int i = ; i <= n; i++)
if(graph[u][i] && dp[i] > dp[u] + )
{
dp[i] = dp[u] + ;
from[i] = u;
que.push(i);
}
} if(dp[ed] == INF) printf("No\n");
else
{
printf("Yes\n");
vector<int> ans;
for(int x = ed; x; x = from[x]) ans.pub(x);
int length = sz(ans);
for(int i = length - ; i >= ; i--)
printf("%d ", ans[i]);
printf("%d\n", ans[]);
}
} int main()
{
freopen("d.in", "r", stdin);
Input();
Solve();
return ;
}