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 ...
随机推荐
- bzoj 1036 Tree Count
题目大意:给出一棵树,每个点有一个权值,要求三种操作:1.修改某个点的权值,2.询问x到y路径上各点的权值最大值,3.询问x到y路径上各点的权值之和. #include <cstdio> ...
- 入门-Arcmap网络分析示例
1.打开arcmap并加载网络数据西安市地图(city.mdb): 它包含的图层有: 2.显示网络中的流向: 3.在设施网络分析工具条上,点选旗标和障碍工具板下拉箭头,将旗标放在city_net_ju ...
- [转]建立swap分区
1,fdisk 时设置id号82(t选项中L可查) 2,mkswap /dev/sdXx 3,swapon /devsdXx 4,cat /proc/swap or swapon -s 就可以看到了 ...
- ZOJ 2672 Fibonacci Subsequence(动态规划+hash)
题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...
- 为什么SQL语句加 1=1
是为了链接下面的查询条件条件,也或者是替换没有查询条件的语句.比如:要把检索条件作为一个参数传递给SQL,那么,当这个检索语句不存在的话就可以给它赋值为1=1.这样就避免了SQL出错,也就可以把加条件 ...
- 让NSURLConnection在子线程中运行
可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...
- 属性的定义以及@synthesize的使用
1.属性通常是指某些由对象封装或储存的数据.它可以是标志(如名称或颜色),也可以是与一个或多个其他对象的关系. 2.属性的基本声明使用 @property 编译器指令,后面紧跟属性的类型信息和名称.您 ...
- Linux下控制器IO地址
在Linux下使用cat /proc/ioports可以查看控制器使用的IO地址范围
- Ubuntu11.10 更新软件源source.list (ZT)
添加完列表后执行 sudo apt-get update sudo apt-get upgrade --------添加列表------------------------------------- ...
- POJ 3278 经典BFS
进一步了解了bfs; 题意:给你n,然后用+,-,*三种运算使n变成k; 漏洞:在算出新的数字之后,一定要判边界,否则RE,而且在每一步后面都得加判断是否等于K,如果是即刻退出,否则WA,判这个的时候 ...