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

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 = 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;
}
时间: 2024-10-07 11:09:08

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

ural 1072. Routing

1072. 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-byt

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

URAL 1934 Black Spot --- 简单最短路变形

边权为1,在维护最短路的同时维护p值最小,我直接存的(1-p),即不遇见的概率,要使得这个值最大. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #includ

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

1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summer camp and now he's a real communication fiend. He spends all his free time on the Web chatting with his friends via ICQ. However, lately the protocol

【BZOJ3889】【Usaco2015 Jan】Cow Routing 双键值最短路

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44064091"); } 题意: 从样例讲起. 第一行 s,t,m表示:起点,终点,m条航线. 然后m组,每组第一行len,n表示这条航线的代价, 这类似于公交车,只要用了就花这些钱,但是用多少都这些钱. 注意是单向边. 举例: 23

DP/最短路 URAL 1741 Communication Fiend

题目传送门 1 /* 2 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 3 正版+破解版->正版,盗版+破解版->盗版 4 DP:每种情况考虑一遍,递推就行了 5 注意:开long long 6 */ 7 #include <cstdio> 8 #include <iostream> 9 #include <algorithm> 10 #include <cstring> 11 #inc

URAL 1056 Computer Net(最短路)

Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecutive computer plug-up to one that has already been connected to the net. Each new computer gets an ordinal number, but the protocol contains the numbe

URAL 1934 最短路变形

DES:给出起点和终点.给出所有小岛的编号.所有路径的起始点.和遇到怪物的概率.要求在最短路的条件下维护遇见怪物的概率最小的路径.就是用 SPFA算法.每条路的权值设为1.最短路即为途径的岛数最少.同时要用pre数组维护每个点的前驱.最后递归输出所走路径.把p变为不遇见怪物的概率, 即为维护p最大.就是把原来的SPFA里的判断多加一条,如果权值相等判断概率,选择概率大的一条. 注意.这是无向图.本来觉得没有影响.很幸福的WA了.确实是.给你a->b的,就代表可以从b->a.而不是简单的可以往返

PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平均值还是一样,就输出按照顺序排列加油站编号最小的. 分析:加油站之间也是彼此有路连接的,所以最短路径计算的时候也要把加油站算上,是双向边,所以边的数组大小得开两倍.加油站编号记为n+1~n+m,对这些点用dijkstra计算最短路径即可,这里顺便在dijkstra中进行了剪枝处理,不剪枝也没事. #