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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
long long yu[N][];
int map[N][N];
int len[N];
int a[N],n;
bool check(int x,int y) {
for(int i=; i<a[x]; i++)
for(int k=; k<a[y]; k++)
if( yu[x][i] == yu[y][k] )
return true;
return false;
}
int pre[N];
void output(int t) {
if( pre[t] == - ) return;
output(pre[t]);
printf(" %d",t);
}
void Dijkstra(int s,int t) {
bool used[N];
int dis[N];
memset(used,false,sizeof(used));
fill(dis,dis+N,INT_MAX);
memset(pre,-,sizeof(pre));
dis[s] = ;
used[s] = true;
int now = s;
for(int i=; i<n; i++) {
for(int k=; k<=n; k++)
if( map[now][k] && dis[k] > dis[now] + ) {
dis[k] = dis[now] + ;
pre[k] = now;
}
int mmin = INT_MAX;
for(int k=; k<=n; k++)
if( !used[k] && dis[k] < mmin )
mmin = dis[now = k];
used[now] = ;
}
if( dis[t] == INT_MAX ) {
printf("No\n");
return ;
}
printf("Yes\n");
printf("%d",s);
output(t);
printf("\n");
}
int main() {
int t1[],t2[],s,t;
scanf("%d",&n);
memset(len,,sizeof(len));
memset(yu,,sizeof(yu));
memset(map,,sizeof(map));
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
for(int k=; k<a[i]; k++) {
scanf("%d.%d.%d.%d",&t1[],&t1[],&t1[],&t1[]);
scanf("%d.%d.%d.%d",&t2[],&t2[],&t2[],&t2[]);
for(int p=; p<; p++) {
yu[i][k] *= ;
yu[i][k] += ( t1[p] & t2[p] );
}
}
for(int k=; k<i; k++)
if( check(k,i) )
map[i][k] = map[k][i] = ;
}
scanf("%d %d",&s,&t);
Dijkstra(s,t); return ;
}

URAL 1072 Routing(最短路)的更多相关文章

  1. ural 1072. Routing

    1072. Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. ...

  2. hdu 1548 A strange lift(迪杰斯特拉,邻接表)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

    1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...

  4. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  5. DP/最短路 URAL 1741 Communication Fiend

    题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...

  6. URAL 1002 Phone Numbers(KMP+最短路orDP)

    In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...

  7. URAL 1085 Meeting(最短路)

    Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...

  8. URAL 1056 Computer Net(最短路)

    Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecu ...

  9. URAL 2034 Caravans(变态最短路)

    Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...

随机推荐

  1. SeGue 多控制器跨界面传递数据原理

    多控制器跨界面传递数据原理

  2. jQuery 元素遍历

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. hdu 1069

    //Accepted 264 KB 0 ms //每种block只有三种方法,且每种放法至多放一次 //规定三条边的顺序后 //把所有的block按x递增排序,x相同则按y递增排序 //然后dp // ...

  4. nslayoutConstraint

    1.vfl的正确编写格式 NSDictionary *dict1 = NSDictionaryOfVariableBindings(_boxV,_headerL,_imageV,_backBtn,_d ...

  5. DDOS攻击原理及防护方法论

      从 07年的爱沙尼亚DDOS信息战,到今年广西南宁30个网吧遭受到DDOS勒索,再到新浪网遭受DDOS攻击无法提供对外服务500多分钟. DDOS愈演愈烈,攻击事件明显增多,攻击流量也明显增大,形 ...

  6. 2016-1-6第一个完整APP 私人通讯录的实现 3:添加联系人

    一:创建模型对象:contact用于存放数据,也便于读取加载 #import <Foundation/Foundation.h> @interface contact : NSObject ...

  7. javaweb-c3p0

    //c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?><c3p0-config> ...

  8. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  9. iOS:测试机添加

    一,首先打开开发者首页:https://developer.apple.com/,点击Member Center二,点击certificates,Identifiers & Profiles三 ...

  10. select2美化下拉单

    http://www.51xuediannao.com/js/jquery/select2.html http://www.51xuediannao.com/demo.php