"Virtual Judge" H-not likely to do approximating a Constant Range
Last Update:2015-12-15
Source: Internet
Author: User
<span id="Label3"></p><p class="pst" style="margin-bottom: 0px;" align="left"><p class="pst" style="margin-bottom: 0px;" align="left"><span style="font-family: Arial;"><span style="font-size: 18pt; color: #0000ff;"><strong>Description</strong></span></span></p></p><p><span style="font-family: ‘Times New Roman‘;"><span style="color: #222222;"><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">in absurdistan, there is</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">N</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">Towns (numbered</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><span style="font-size: 12.6pt;">1</span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">through</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">N</span></em></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">)</span> and</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">m</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">bidirectional Railways. There is also a absurdly simple road network-for each pair of different towns</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">x</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;"></span> and</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">y</span></em></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">, there is a bidirectional road between towns</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">x</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;"></span> and</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">y</span></em></span></span><span class="tex-font-style-bf"><span class="tex-font-style-bf"><span style="font-size: 10.2pt;">if</span></span> and only if</span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">There is no railway between Them. <span class="Apple-converted-space"> </span> Travelling to a different the town using one railway or one road always takes exactly one hour.</span></span></span></span></p><p><span style="font-family: ‘Times New Roman‘;"><span style="color: #222222;"><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">a train and a bus leave</span> town</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><span style="font-size: 12.6pt;">1</span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;">At the <span style="font-size: 10.2pt;">same Time. They both has the same destination,</span> town</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">N</span></em></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">, and don ' t make any stops on the the The-the-the-same-they</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">N</span></em></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">). The train can move only along railways and the bus can move only along roads.</span></span></span></span></p><p><p> <span style="font-family: ' Times New Roman ';"><span style="color: #222222;"><span style="font-size:10.2pt;">you ' ve been asked to plan out routes for the vehicles; Each route can use any road/railway multiple times. One of the most important aspects to consider are safety-in order to avoid accidents at railway crossings, the train and The bus must not arrive in the same town (except town </span> <span class="apple-converted-space"><span style="font-size:10 .2pt; "> </span> </span> <span class="tex-span"> <em> <span style="font-size:12.6pt;">n </span> </em> </span> <span style="font-size:10.2pt;">) simultaneously. </span> </span> </span> </p></p><p><span style="font-family: ‘Times New Roman‘;"><span style="font-family: ' Times New Roman ';"><span style="color: #222222;"><span style="font-size:10.2pt;">under These constraints, what's the minimum number of hours needed for both vehicles to reach town </span> <span class="apple-co Nverted-space "><span style=" font-size:10.2pt; "> </span> </span> <span class=" tex-span "> <em> <span style=" font-size:12.6pt; ">n </span> </em> </span> <span class="apple-converted-space"><span style="font-size:10.2pt;"> </span> </span> <span style="font-size:10.2pt;" (the maximum of arrival times the bus and train)? note, that train is not required to arrive town < span> <span class="apple-converted-space"><span style="fon t-size:10.2pt; "> </span> </span> <span class="tex-span"> <em> <span style="font-size:12.6pt;">n </span> </em> </span> <span class="apple-converted-space"><span style="font-size:10.2pt;"> </span> </span> <span style="font-size:10.2pt;">at The same moment of time, but is allowed to doing so. </span> </span> </span></span></span></p><p class="pst" style="margin-bottom: 0px;" align="left"><p class="pst" style="margin-bottom: 0px;" align="left"><span style="font-family: Arial;"><span style="font-size: 18pt; color: #0000ff;"><strong>Input</strong></span></span></p></p><p><span style="font-family: ‘Times New Roman‘;"><span style="color: #222222;"><span style="font-size: 10.2pt;">The first line of the <span style="font-size: 10.2pt;">input contains the integers</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">N</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;"></span> and</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">m</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">(</span></span><span class="tex-span"><span class="tex-span"><span style="font-size: 12.6pt;">2≤ <em>n</em> ≤400</span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">,</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><span style="font-size: 12.6pt;">0≤ <em>m</em> ≤ <em>n</em>(<em>n</em> -1)/2</span></span></span><span style="font-size: 10.2pt;">)-the number of towns and the number of <span style="font-size: 10.2pt;">Railways Respectively.</span></span></span></span></p><p><span style="font-family: ‘Times New Roman‘;"><span style="color: #222222;"><span style="font-size: 10.2pt;">Each of the <span style="font-size: 10.2pt;">next</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">m</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">lines contains, integers</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">u</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;"></span> and</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">v</span></em></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">, denoting a railway between towns</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">u</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;"></span> and</span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"><em><span style="font-size: 12.6pt;">v</span></em></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">(</span></span><span class="tex-span"><span class="tex-span"> <span style="font-size: 12.6pt;">1≤ <em>u</em>, <em>v</em> ≤ </span> <em><span style="font-size: 12.6pt;">n</span></em> </span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">,</span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span></span><span class="tex-span"><span class="tex-span"> <em><span style="font-size: 12.6pt;">u</span></em> <span style="font-size: 12.6pt;"> ≠ </span> <em><span style="font-size: 12.6pt;">v</span></em> </span></span><span style="font-size: 10.2pt;"><span style="font-size: 10.2pt;">).</span></span></span></span></p><p><p><span style="font-family: ‘Times New Roman‘;"><span style="font-size: 10.2pt; color: #222222;">Assume that there are at the most one railway connecting any and Towns.</span></span></p></p><p class="pst" style="margin-bottom: 0px;" align="left"><p class="pst" style="margin-bottom: 0px;" align="left"><span style="font-family: Arial;"><span style="font-size: 18pt; color: #0000ff;"><strong>Output</strong></span></span></p></p><p><p> <span style="font-family: ' Times New Roman ';"><span style="color: #222222;"><span style="font-size:10.2pt;">output One integer-the smallest possible time of the later vehicle ' s arrival in town </span> <span class="apple-converted-space"><span style="font-size:10.2pt;"> </span> </span> <span class="tex-span"> <em> <span style="font-size:12.6pt;" n < span> </span></em> </span> <span style="font-size:10.2pt;" . if it ' s impossible for at least one of the vehicles to reach town < span> <span class="apple-converted-space"><span style="f ont-size:10.2pt; "> </span> </span> <span class="tex-span"> <em> <span style="font-size:12.6pt;">n </span> </em> </span> <span style="font-size:10.2pt;">, output </span> <span class="apple-converted-space"><span style="font-size:10.2pt;"> </span> </span> <span class="tex-span"><span style="font-size:12.6pt;" -1 < span> </span> <span style="font-size:10.2pt;" . < span> </span> </span></span></span></span></p></p><p class="pst" style="margin-bottom: 0px;" align="left"><p class="pst" style="margin-bottom: 0px;" align="left"><span style="font-family: Arial;"><span style="font-size: 18pt; color: #0000ff;"><strong>Sample Input</strong></span></span></p></p><span style="font-family: Arial;"><span style="font-family: Arial;"><span style="font-size: 10.2pt; color: #222222;"><strong>Input</strong></span></span></span><span style="font-size: 10.2pt; color: #222222;"><span style="font-size: 10.2pt; color: #222222;">4 2<br>1 3<br>3 4</span></span><span style="font-family: Arial;"><span style="font-family: Arial;"><span style="font-size: 10.2pt; color: #222222;"><strong>Output</strong></span></span></span><span style="font-size: 10.2pt; color: #222222;"><span style="font-size: 10.2pt; color: #222222;">2</span></span><span style="font-family: Arial;"><span style="font-family: Arial;"><span style="font-size: 10.2pt; color: #222222;"><strong>Input</strong></span></span></span><span style="font-size: 10.2pt; color: #222222;"><span style="font-size: 10.2pt; color: #222222;">4 6<br>1 2<br>1 3<br>1 4<br>2 3<br>2 4<br>3 4</span></span><span style="font-family: Arial;"><span style="font-family: Arial;"><span style="font-size: 10.2pt; color: #222222;"><strong>Output</strong></span></span></span><span style="font-size: 10.2pt; color: #222222;"><span style="font-size: 10.2pt; color: #222222;">-1</span></span><span style="font-family: Arial;"><span style="font-family: Arial;"><span style="font-size: 10.2pt; color: #222222;"><strong>Input</strong></span></span></span><span style="font-size: 10.2pt; color: #222222;"><span style="font-size: 10.2pt; color: #222222;">5 5<br>4 2<br>3 5<br>4 5<br>5 1<br>1 2</span></span><span style="font-family: Arial;"><span style="font-family: Arial;"><span style="font-size: 10.2pt; color: #222222;"><strong>Output</strong></span></span></span><span style="font-size: 10.2pt; color: #222222;"><span style="font-size: 10.2pt; color: #222222;">3</span></span><p class="pst" style="margin-bottom: 0px;" align="left"><p class="pst" style="margin-bottom: 0px;" align="left"><span style="font-family: Arial;"><span style="font-size: 18pt; color: #0000ff;"><strong>Hint</strong></span></span></p></p><p><p> <span style="font-family: ' Times New Roman ';"><span style="color: #222222;"><span style="font-size:10.2pt;">in The first sample, the train can take the Route<span class="apple-converted-space"> </span> <span class=" Apple-converted-space "> </span> and the bus can take the Route<span class=" apple-converted-space "> </span>. Note that they can arrive at the town </span> <span class="tex-span"><span style="font-size:12.6pt;">4 </span> </span> <span class="apple-converted-space"><span style="font-size:10.2pt;"> </span> </span> <span style="font-size:10.2pt;">at the same time. </span> </span> </span> </p></p><p><p><span style="font-family: ‘Times New Roman‘;"><span style="color: #222222;"><span style="font-size: 10.2pt;">in the second sample, Absurdistan are ruled by Railwaymen. There is no roads, so there's no-for-the-bus-to-reach</span> <span class="Apple-converted-space"><span style="font-size: 10.2pt;"> </span></span>town <span class="tex-span"><span style="font-size: 12.6pt;">4</span></span><span style="font-size: 10.2pt;">.</span></span></span></p></p><p><p>Because 1 to n cities must have a road, either a railway or a road, is a railway, the shortest way to find the road, is the road to find the shortest line of the railway</p></p><pre class="brush:cpp;gutter:true;">#include <stdio.h>const int Maxn=0x7fff;long long n,m,s,e,t[405][405],u[405][405],dist[405];void Dijk (int v0, Long Long r[][405]) {bool b[405]; For (int i=2; i<=n; i++) {dist[i]=r[v0][i]; b[i]=false; } dist[v0] = 0; b[v0] = true; For (int i=2; i<=n; i++) {long Long mindis=maxn; int u = v0; For (int j=1; j<=n; j + +) if ((!b[j]) && dist[j]<mindis) {u = j; Mindis = dist[j]; } b[u]=true; For (int j=1; j<=n; j + +) if ((!b[j]) && r[u][j]<maxn) if (dist[u] + r[u][j] < dist [j]) dist[j] = dist[u] + r[u][j]; }}int main () {scanf ("%lld%lld", &n,&m); For (int i=0, i<=n; i++) for (int j=0; j<=n; j + +) {t[i][j]=maxn; u[i][j]=1; } for (int i=0; i<m; i++) {scanf ("%lld%lld", &s,&e); T[s][e]=t[e][s]=1; u[s][e]=u[e][s]=maxn; } if (u[1][n]==1)//road direct, shortest route Dijk (1,t) of the railway; else Dijk (1,u); If (dist[n]>=maxn) printf (" -1\n"); else printf ("%lld\n", dist[n]); Return 0;}</pre><p><p> </p></p><p><p>"Virtual Judge" H-not likely to do approximating a Constant Range</p></p></span>