URAL 1072 Routing(最短路)
Routing
Memory limit: 64 MB
- 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 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). - 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.
- A packet is transmitted between two computers of one subnet directly.
- 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.
Input
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.
Output
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 |
Yes |
#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(最短路)的更多相关文章
- ural 1072. Routing
1072. Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. ...
- hdu 1548 A strange lift(迪杰斯特拉,邻接表)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)
1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...
- 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 ...
- DP/最短路 URAL 1741 Communication Fiend
题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...
- 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 ...
- URAL 1085 Meeting(最短路)
Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...
- URAL 1056 Computer Net(最短路)
Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecu ...
- URAL 2034 Caravans(变态最短路)
Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...
随机推荐
- ios开发逆向传值的几种方法整理
第一种:代理传值 第二个控制器: @protocol WJSecondViewControllerDelegate <NSObject> - (void)changeText:(NSStr ...
- scst使用的一些问题
1,编译问题 问题描述: [root@localhost scstadmin]# make cd scstadmin && make all ]: Entering directory ...
- linux之开发板与宿主机通信--ftp使用
在目标板终端上输入命令: # ftp 192.16.77.66 //192.16.77.66是宿主机IP # cd /home/ //这里可以使用linux命令,但不能使有TAB键 # get ...
- 第四课 Gallery的使用
直接上代码 1.Layout--Main.axml <?xml version="1.0" encoding="utf-8"?> <Linea ...
- poj1181 大数分解
//Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...
- ACM - KMP题目小结 (更新中)
KMP算法题型大致有两类,一类是next数组的应用,一类是匹配问题. next数组大多数是求字符串周期,或者是与前缀后缀有关,也可以应用在DP中.需要对next数组有一定理解才能做得出. next数组 ...
- sql server和oracle的差异
.部分SQL语句差异 (1)SQL:select top 10 * from table ORA: select * from table where rownum<11(2)SQL:S ...
- 如何安装php?
1.解压apache文件 2.进行注册,写地址,邮箱 3.下一步选择Typical 4.下一步随意选个安装路径 5.解压php文件 6.把php.ini-development文件改为php.ini ...
- JQuery源码分析(三)
jQuery中ready与load事件 jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //document read ...
- 一个经典例子让你彻彻底底理解java回调机制
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢 所谓回调: ...