PHP 对数字进行 简单的加密解密
Connections between cities
Time
Limit: 10000/5000 MS (Java/Others) Memory Limit:
32768/32768 K (Java/Others)
Total Submission(s):
3937 Accepted Submission(s):
1144
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 |
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<vector> #include<stdlib.h> #include<set> #include<map> #include<cmath> using
namespace std; #define maxn 10000+5 int
pre[maxn][20],vit[maxn],n,m,c,deep[maxn]; __int64
lenth[maxn][20]; vector< int
>ko[maxn],dis[maxn]; void
dfs( int
x, int de) { vit[x]=1; deep[x]=de; for ( int
i=0; i<ko[x].size(); i++) { int
to=ko[x][i]; if (!vit[to]) { pre[to][0]=x; lenth[to][0]=dis[x][i]; dfs(to,de+1); } } } int
main() { int
i,j; while (cin>>n>>m>>c) { for (i=1; i<=n; i++) { ko[i].clear(); dis[i].clear(); } for (i=1; i<=m; i++) { int
u,v,l; scanf ( "%d%d%d" ,&u,&v,&l); ko[u].push_back(v); dis[u].push_back(l); ko[v].push_back(u); dis[v].push_back(l); } memset (vit,0, sizeof (vit)); memset (pre,-1, sizeof (pre)); memset (lenth,0, sizeof (lenth)); memset (deep,0, sizeof (deep)); for (i=1; i<=n; i++) if (!vit[i]) { pre[i][0]=0; dfs(i,1); } for (i=1; i<20; i++) for (j=1; j<=n; j++) { if (pre[j][i-1]!=-1) { pre[j][i]=pre[ pre[j][i-1] ][i-1]; lenth[j][i]=lenth[j][i-1]+lenth[ pre[j][i-1] ][i-1]; } } while (c--) { int
u,v,dd,num=0; __int64
mm=0; scanf ( "%d%d" ,&u,&v); if (deep[u]<deep[v]) swap(u,v); dd=deep[u]-deep[v]; while (dd) //使 u v处于同一点 { if (dd&1) { mm+=lenth[u][num]; u=pre[u][num]; } ++num; dd>>=1; } if (u==v) { if (u>0) printf ( "%I64d\n" ,mm); else printf ( "Not connected\n" ); continue ; } while (u!=v) { int
If; for (i=0; i<20; i++) { if (pre[u][i]==pre[v][i]) { If=i; break ; } } if (If==0) { if (pre[u][0]>0) { mm+=lenth[u][0]+lenth[v][0]; printf ( "%I64d\n" ,mm); } else printf ( "Not connected\n" ); break ; } else { mm+=lenth[u][If-1]+lenth[v][If-1]; u=pre[u][If-1]; v=pre[v][If-1]; } } } } } |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。