ural 1072. Routing

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

Tags: graph theory  (hide tags for unsolved problems)

Difficulty: 464

题意:给出n台计算机,每台计算机有若干对IP地址以及子网掩码,当且仅当存在(IP地址i AND 子网掩码i) = (IP地址j AND 子网掩码j)时,两台计算机才可以联系。

问从一台计算机到另一台计算机最短要经过多少计算机。

输出方案。

分析:实际上就是最短路。

  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define MIT (2147483647)
 21 #define INF (1000000001)
 22 #define MLL (1000000000000000001LL)
 23 #define sz(x) ((int) (x).size())
 24 #define clr(x, y) memset(x, y, sizeof(x))
 25 #define puf push_front
 26 #define pub push_back
 27 #define pof pop_front
 28 #define pob pop_back
 29 #define ft first
 30 #define sd second
 31 #define mk make_pair
 32
 33 inline int Getint()
 34 {
 35     int Ret = 0;
 36     char Ch = ‘ ‘;
 37     bool Flag = 0;
 38     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
 39     {
 40         if(Ch == ‘-‘) Flag ^= 1;
 41         Ch = getchar();
 42     }
 43     while(Ch >= ‘0‘ && Ch <= ‘9‘)
 44     {
 45         Ret = Ret * 10 + Ch - ‘0‘;
 46         Ch = getchar();
 47     }
 48     return Flag ? -Ret : Ret;
 49 }
 50
 51 const int N = 110;
 52 int n;
 53 int length[N];
 54 vector<unsigned int> feature[N];
 55 bool graph[N][N];
 56 queue<int> que;
 57 int dp[N], from[N], st, ed;
 58
 59 inline unsigned int Get()
 60 {
 61     const static unsigned int fact[4] = {16777216, 65536, 256, 1};
 62     unsigned int ret = 0;
 63     for(int i = 0; i < 4; i++)
 64     {
 65         int x = (unsigned int) Getint();
 66         ret += x * fact[i];
 67     }
 68     return ret;
 69 }
 70
 71 inline void Input()
 72 {
 73     scanf("%d", &n);
 74     for(int i = 1; i <= n; i++)
 75     {
 76         scanf("%d", &length[i]);
 77         //cout << i << ": ";
 78         for(int j = 1; j <= length[i]; j++)
 79         {
 80             unsigned int x, y;
 81             x = Get();
 82             y = Get();
 83             feature[i].pub(x & y);
 84             //cout << (x & y) << ‘ ‘;
 85         }
 86         //cout << endl;
 87     }
 88     scanf("%d%d", &st, &ed);
 89 }
 90
 91 inline bool find(int x, int y)
 92 {
 93     int len1 = sz(feature[x]), len2 = sz(feature[y]);
 94     for(int i = 0; i < len1; i++)
 95         for(int j = 0; j < len2; j++)
 96             if(feature[x][i] == feature[y][j])
 97                 return 1;
 98     return 0;
 99 }
100
101 inline void Solve()
102 {
103     for(int i = 1; i <= n; i++)
104         for(int j = 1; j <= n; j++)
105             if(i != j && find(i, j))
106                 graph[i][j] = 1;
107
108     for(int i = 1; i <= n; i++)
109         from[i] = 0, dp[i] = INF;
110     dp[st] = 0;
111     que.push(st);
112     while(sz(que))
113     {
114         int u = que.front();
115         que.pop();
116         for(int i = 1; i <= n; i++)
117             if(graph[u][i] && dp[i] > dp[u] + 1)
118             {
119                 dp[i] = dp[u] + 1;
120                 from[i] = u;
121                 que.push(i);
122             }
123     }
124
125     if(dp[ed] == INF) printf("No\n");
126     else
127     {
128         printf("Yes\n");
129         vector<int> ans;
130         for(int x = ed; x; x = from[x]) ans.pub(x);
131         int length = sz(ans);
132         for(int i = length - 1; i >= 1; i--)
133             printf("%d ", ans[i]);
134         printf("%d\n", ans[0]);
135     }
136 }
137
138 int main()
139 {
140     freopen("d.in", "r", stdin);
141     Input();
142     Solve();
143     return 0;
144 }

时间: 2024-10-21 02:18:37

ural 1072. Routing的相关文章

URAL 1072 Routing(最短路)

Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. It means that: Each computer has one or more net interfaces. Each interface is identified by its IP-address and a subnet mask - these are two four-byte numb

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

解决vSphere 5.1上Linux VM提示:Unable to collect IPv4 routing table问题

错误信息:vmsvc[xxxx]:[warning][guestinfo]RecordRoutingInfo:Unable to collect IPv4 routing table 经检查确认,下列组件正确安装:LinuxVMOS正确安装:IP信息正确设定:VMwareTools正确安装:虚拟机硬件版本为9: 重启VM的时候,启动过程中提示libguestinfo.so模块故障会提示,不过VM依然正常启动,不过它会Hung住1.20分钟: 通过故障提示,经过系列排查之后,发现如果去掉libti

Flask error: werkzeug.routing.BuildError

@main.route('/sendfile', methods=['GET', 'POST']) def sendfile():     if request.method == 'POST':         f = request.files['file']         basepath = path.abspath(path.dirname(__file__))         upload_path = path.join(basepath, 'static/uploads')  

RabbitMQ 路由选择 (Routing)

让日志接收者能够订阅部分消息.例如,我们可以仅仅将致命的错误写入日志文件,然而仍然在控制面板上打印出所有的其他类型的日志消息. 1.绑定(Bindings) 在前面中我们已经使用过绑定.类似下面的代码: 1 channel.queueBind(queueName, EXCHANGE_NAME, ""); 绑定表示转发器与队列之间的关系.我们也可以简单的认为:队列对该转发器上的消息感兴趣. 绑定可以附带一个额外的参数routingKey.为了与避免basicPublish方法(发布消息的

ip routing&no ip routing

ip routing--------查路由表, 如果ping的目的在RT中没有,不发出任何包(arp也不会发出)?? 如果RT中存在,则arp??下一跳,相当于no ip routing+配置网关 no ip routing----不查路由表? ?? ?? ?? ?不配置网关---arp-catch中存在很多条目(相当于static指出接口)? ?? ?? ?? ?配置网关--则会arp网关一次,有一个条目 ip routing&no ip routing

BZOJ 1072 [SCOI2007]排列perm

考虑到s的长度特别小,只有10,可以考虑状压dp. 设F[S][d]表示当选了集合S(用二进制压位表示)中的所有位置,对D取模的结果为d的方案总数:不难想到转移和初始化. 初始化:F[0][0]=1  0在这里表示空集合 转移:F[S][(d * 10 + s[i]-'0') % D]=sum{F[S0][d]}  S0是S的一个子集,并且刚好只比S少一个元素i 注意,重复的数字被算了多遍.样例当中就有.因此最后的答案要除以所有重复的数字个数的阶乘. 看代码就明白啦~ (再补充一个要用到状压技巧

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is