URAL 1072 Routing(最短路)

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

inputoutput
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 = 105;
const int M = 24005;
long long yu[N][6];
int map[N][N];
int len[N];
int a[N],n;
bool check(int x,int y) {
    for(int i=0; i<a[x]; i++)
        for(int k=0; 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] == -1 ) 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,-1,sizeof(pre));
    dis[s] = 0;
    used[s] = true;
    int now = s;
    for(int i=0; i<n; i++) {
        for(int k=1; k<=n; k++)
            if( map[now][k] && dis[k] > dis[now] + 1 ) {
                dis[k] = dis[now] + 1;
                pre[k] = now;
            }
        int mmin = INT_MAX;
        for(int k=1; k<=n; k++)
            if( !used[k] && dis[k] < mmin )
                mmin = dis[now = k];
        used[now] = 1;
    }
    if( dis[t] == INT_MAX ) {
        printf("No\n");
        return ;
    }
    printf("Yes\n");
    printf("%d",s);
    output(t);
    printf("\n");
}
int main() {
    int t1[5],t2[5],s,t;
    scanf("%d",&n);
    memset(len,0,sizeof(len));
    memset(yu,0,sizeof(yu));
    memset(map,0,sizeof(map));
    for(int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
        for(int k=0; k<a[i]; k++) {
            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++) {
                yu[i][k] *= 1000;
                yu[i][k] += ( t1[p] & t2[p] );
            }
        }
        for(int k=1; k<i; k++)
            if( check(k,i) )
                map[i][k] = map[k][i] = 1;
    }
    scanf("%d %d",&s,&t);
    Dijkstra(s,t);

    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6009759.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值