zoukankan      html  css  js  c++  java
  • 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
    ");
            return ;
        }
        printf("Yes
    ");
        printf("%d",s);
        output(t);
        printf("
    ");
    }
    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;
    }
  • 相关阅读:
    apt常用命令(安装,更新,删除)
    记录一次坑爹的VM连接主机的路程
    VM安装centos
    初窥DB2之insert语句
    关于虚拟机的linux不能使用shell连接时的处理方法
    linux命令之查看字符集
    趣图:学JavaScript
    PHP搭建大文件切割分块上传功能示例
    判断变量是否不为空,函数isset()、!empty()与!is_null()的比较
    Javascript 中 null、NaN和undefined的区别
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6009759.html
Copyright © 2011-2022 走看看