有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来。现在,小明在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设你不走重复的路)。
第一行输入一个整数M表示测试数据共有M(1<=M<=5)组
每组测试数据的第一行输入一个正整数N(1<=N<=100000)和一个正整数S(1<=S<=100000),N表示城市的总个数,S表示参观者所在城市的编号
随后的N-1行,每行有两个正整数a,b(1<=a,b<=N),表示第a号城市和第b号城市之间有一条路连通。
每组测试数据输N个正整数,其中,第i个数表示从S走到i号城市,必须要经过的上一个城市的编号。(其中i=S时,请输出-1)
1
10 10
1 2
1 9
1 8
3 7
8 6
8 10
9 5
10 3
10 4
8 1 10 10 9 8 3 10 1 -1
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<queue>
4 #include<algorithm>
5 #define MIN(x,y)(x<y?x:y)
6 using namespace std;
7 const int INF=0x3f3f3f3f;
8 const int MAXN=100010;
9 const int MAXM=200010;
10 int top,N,S;
11 struct Edge
12 {
13 int from,to,val,next;
14 };
15 Edge edg[MAXM];
16 int head[MAXM],dis[MAXN],vis[MAXN];
17 void add(int u,int v,int w)
18 {
19 Edge E={u,v,w,head[u]};
20 edg[top]=E;
21 head[u]=top++;
22 }
23 int SPFA(int now)
24 {
25 queue<int>q;
26 memset(vis,0,sizeof(vis));
27 memset(dis,INF,sizeof(dis));
28 q.push(S);
29 dis[S]=0;
30 vis[S]=1;
31 while(!q.empty())
32 {
33 int u=q.front();
34 q.pop();
35 vis[u]=0;
36 for(int i=head[u];i!=-1;i=edg[i].next)
37 {
38 int v=edg[i].to;
39 if(dis[v]>dis[u]+edg[i].val)
40 {
41 dis[v]=dis[u]+edg[i].val;
42 if(v==now)return u;
43 if(!vis[v])
44 {
45 vis[v]=1;
46 q.push(v);
47 }
48 }
49 }
50 }
51 }
52 int main(){
53 int M,a,b;
54 scanf("%d",&M);
55 while(M--){
56 top=0;
57 scanf("%d%d",&N,&S);
58 memset(head,-1,sizeof(head));
59 for(int i=0;i<N-1;i++){
60 scanf("%d%d",&a,&b);
61 add(a,b,1);
62 add(b,a,1);
63 }
64 for(int i=1;i<=N;i++){
65 if(i!=1)printf(" ");
66 if(i==S){
67 printf("-1");continue;
68 }
69 int x=SPFA(i);
70 printf("%d",x);
71 }
72 puts("");
73 }
74 return 0;
75 }
问题 J: Hidden Number
时间限制: 1 Sec 内存限制: 32 MB
提交: 25 解决: 4
[提交][状态][讨论版]
题目描述
Your job is to find out the secret number hidden in a matrix, each of whose element is a digit (‘0‘-‘9‘) or a letter (‘A‘-‘Z‘). You can see an example matrix in Figure 1.
The hidden number and other non-secret ones are coded in a matrix as sequences of digits in a decimal format. You should only consider sequences of digits D1 D2 ... Dn such that Dk+1 (1 <= k < n) is either right next to or immediately below Dk in the matrix. The secret you are seeking is the largest number coded in this manner.
Four coded numbers in the matrix in Figure 1, i.e., 908820, 23140037, 23900037, and 9930, are depicted in Figure 2. As you may see, in general, two or more coded numbers may share a common subsequence. In this case, the secret number is 23900037, which is the largest among the set of all coded numbers in the matrix.
In contrast, the sequences illustrated in Figure 3 should be excluded: 908A2 includes a letter; the fifth digit of 23149930 is above the fourth; the third digit of 90037 is below right of the second.
Write a program to figure out the secret number from a given matrix.
输入
The input consists of multiple data sets, each data set representing a matrix. The format of each data
set is as follows.
W H
C11C12 ... C1W
C21C22 ... C2W
...
CH1CH2 ... CHW
In the first line of a data set, two positive integers W and H are given. W indicates the width (the number
of columns) of the matrix, and H indicates the height (the number of rows) of the matrix. W+H is less
than or equal to 70.
H lines follow the first line, each of which corresponds to a row of the matrix in top to bottom order. The
i-th row consists of W characters Ci1Ci2 ... CiW in left to right order. You may assume that the matrix
includes at least one non-zero digit.
Following the last data set, two zeros in a line indicate the end of the input.
输出
For each data set, print the hidden number on a line. Leading zeros should be suppressed.
样例输入
7 4
9R2A993
0E314A0
8A900DE
820R037
6 7
JH03HE
ID7722
0DA1AH
30C9G5
99971A
CA7EAI
AHLBEM
20 2
A1234567891234CBDEGH
BDEDF908034265091499
0 0
样例输出
23900037
771971
12345908034265091499
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<queue>
4 using namespace std;
5 int vis[80][80];
6 struct Node{
7 int x,y;
8 char s[80];
9 };
10 queue<Node>dl;
11 Node a,b;
12 char map[80][80];
13 int disx[2]={0,1};
14 int disy[2]={1,0};
15 int W,H;
16 bool judge(char *a,char *b){
17 int t1,t2;
18 t1=strlen(a);t2=strlen(b);
19 if(t1>t2)return true;
20 else if(t1<t2)return false;
21 else if(strcmp(a,b)<0)return false;
22 else return true;
23 }
24 char ans[80];
25 void cat(char *a,char b){
26 int t1=strlen(a);
27 a[t1]=b;
28 a[t1+1]=‘\0‘;
29 }
30 void bfs(int sx,int sy){
31 memset(vis,0,sizeof(vis));
32 memset(a.s,0,sizeof(a.s));
33 memset(b.s,0,sizeof(b.s));
34 a.x=sx;a.y=sy;
35 cat(a.s,map[sx][sy]);
36 vis[sx][sy]=1;
37 dl.push(a);
38 while(!dl.empty()){
39 a=dl.front();
40 dl.pop();
41 vis[a.x][a.y]=1;
42 if(judge(a.s,ans))strcpy(ans,a.s);
43 for(int i=0;i<2;i++){
44 b.x=a.x+disx[i];b.y=a.y+disy[i];
45 if(!vis[b.x][b.y]&&b.x>=0&&b.x<H&&b.y>=0&&b.y<W&&map[b.x][b.y]>=‘0‘&&map[b.x][b.y]<=‘9‘)
46 {
47 strcpy(b.s,a.s);
48 cat(b.s,map[b.x][b.y]);
49 dl.push(b);
50 }
51 }
52 }
53 }
54 int main(){
55 while(~scanf("%d%d",&W,&H),W||H){
56 memset(ans,0,sizeof(ans));
57 while(!dl.empty())dl.pop();
58 for(int i=0;i<H;i++)scanf("%s",map[i]);
59 for(int i=0;i<H;i++)
60 for(int j=0;j<W;j++){
61 if(map[i][j]>‘0‘&&map[i][j]<=‘9‘)bfs(i,j);
62 while(!dl.empty())dl.pop();
63 }
64 printf("%s\n",ans);
65 }
66 return 0;
67 }