In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.
A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.
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 is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.
Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.
The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.
Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n, u ≠ v).
You may assume that there is at most one railway connecting any two towns.
Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output - 1.
4 2
1 3
3 4
2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
-1
5 5
4 2
3 5
4 5
5 1
1 2
3
In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town4 at the same time.
In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.
有n个点,n*(n-1)/2条边,任意两个点之间都有边,现在把边黑白染色,有m条白的边,其他都黑色的。两个人一开始位于1号点,第一个人只走白边,第二个人只走黑边,问两个人都到达n的最短时间。有一个人不能到达就输出-1。可以不同步,就是不必同时到达n
一开始我这想半天。。还想过爆搜。。主要是题面太有误导性
后来突然醒悟,一定有一条1到n的,不管是黑色还是白色
然后只要看另一个人什么时候到就好啦
这题n才400这么小,都能folyd了
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define pi 3.1415926535897932384626433832795028841971 16 using namespace std; 17 inline LL read() 18 { 19 LL x=0,f=1;char ch=getchar(); 20 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 21 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 22 return x*f; 23 } 24 inline void write(LL a) 25 { 26 if (a<0){printf("-");a=-a;} 27 if (a>=10)write(a/10); 28 putchar(a%10+'0'); 29 } 30 inline void writeln(LL a){write(a);printf("\n");} 31 int n,m; 32 int dis[401][401]; 33 bool mrk[401][401],fuckyou; 34 int main() 35 { 36 n=read();m=read(); 37 memset(dis,127/3,sizeof(dis)); 38 for (int i=1;i<=m;i++) 39 { 40 int x=read(),y=read(); 41 mrk[x][y]=mrk[y][x]=1; 42 } 43 if (mrk[1][n])fuckyou=1; 44 for (int i=1;i<=n;i++) 45 for (int j=1;j<=n;j++) 46 if (fuckyou^mrk[i][j])dis[i][j]=1ll; 47 for (int k=1;k<=n;k++) 48 for (int i=1;i<=n;i++) 49 for (int j=1;j<=n;j++) 50 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 51 if (dis[1][n]>=1000000)printf("-1"); 52 else printf("%d\n",dis[1][n]); 53 }