3417: Poi2013 Tales of seafaring

来源:互联网 发布:旺商云订单打印软件 编辑:程序博客网 时间:2024/03/29 17:16

3417: Poi2013 Tales of seafaring

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 377  Solved: 121
[Submit][Status][Discuss]

Description

Young Bytensson loves to hang out in the port tavern, where he often listens to the sea dogs telling their tales of seafaring. Initially, he believed them all, however incredible they sounded. Over time though, he became suspicious. He has decided to write a program that will verify if there may be any grain of truth in those tall stories. Bytensson reasoned that while he cannot tell if the sailors indeed weathered all those storms, he can at least find out if their travel itineraries make sense. This is a task for a programmer, which Bytensson, unfortunately, is not. Help him out!
There are   ports and   waterways connecting them in the waters frequented by the sailors Bytensson listened to. If there is a waterway between two ports, then sailing from one to the other is possible. Any waterway can be sailed in both directions.
Bytensson got to know K seafaring tales. Each tells of a sailor who began his journey in one port, sailed a number of waterways, and ended up in another port, which may have been the one he initially set sail from. The sailor in question may have sailed through the same waterway many times, each time in any direction.


一个nm边无向图,边权均为1,有k个询问

每次询问给出(s,t,d),要求回答是否存在一条从st的路径,长度为d

路径不必是简单路(可以自交)

Input

In the first line of the standard input, there are three integers, N,M and K (2<=N<=5000,1<=M<=5000,1<=K<=1000000) These denote, respectively: the number of ports in the waters frequented by the sailors who told Bytensson their stories, the number of waterways, and the number of tales.
The M lines that follow specify the waterways. A single waterway's description consists of a single line that contains two integers, a and b (1<=a,b<=N,a<>b) separated by a single space; these specify the numbers of ports at the two ends of this particular waterway.
The K lines that follow specify the tales that Bytensson has heard. A single tale's description consists of a single line with three integers, s,t  and d (1<=S,T<=N,1<=d<=1000000000) separated by single spaces. These indicate that the tale's protagonist set sail from port no. s, ended the journey in port no. t, and sailed exactly d times through various waterways.

Output

Your program should print exactly K lines to the standard output; the i-th of them should contain the word TAK (Polish for yes) if the journey described in the i-th tale (in input order) could have taken place. If it could not, then the line should contain the word NIE(Polish for no).

Sample Input

8 7 4
1 2
2 3
3 4
5 6
6 7
7 8
8 5
2 3 1
1 4 1
5 5 8
1 8 10

Sample Output

TAK
NIE
TAK
NIE

HINT

Source

鸣谢Wcmg提供译文

[Submit][Status][Discuss]



非简单路径,可以自交,说明只要存在,就可以很方便地构造出一条符合询问的路径

就是先走到终点,然后前一条边不断走回来走过来。。。。。。

不过,这样操作的是对于当前选择的路径,权值+2+2+2+2+2......

于是发现一个很严肃的问题,得和目标长度同奇偶性才能这样瞎搞

那么定义dis[i][j][2]:i,j之间,长度为奇数/偶数的最短路径的长度

显然,,只要满足了奇偶性,路径越短越好,因为总是能构造到满足题意

又,,原图每条边长度为1,dis的计算可以直接用BFS解决,复杂度O(n^2*2)

但是,,此题空间限制卡得紧,可以考虑dis数组只储存一半,储存的值采用short

最后,,注意特判路径根本不存在的情况= =

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<queue>#include<set>#include<map>#include<stack>#include<bitset>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const short N = 5001;const short INF = 32767;struct data{short A,B; data(){}data(short A,short B): A(A),B(B){}};struct Point{short x,t; Point(){}Point(short x,short t): x(x),t(t){}};int n,m,q,du[N],dis[N][2];vector <short> v[N];vector <data> G[N];queue <Point> Q;int getint(){char ch = getchar(); int ret = 0;while (ch < '0' || '9' < ch) ch = getchar();while ('0' <= ch && ch <= '9')ret = ret*10 + ch - '0',ch = getchar();return ret;}void BFS(int k){for (int i = 1; i <= n; i++)for (int j = 0; j < 2; j++) dis[i][j] = INF;dis[k][0] = 0; Q.push(Point(k,0));while (!Q.empty()){int x = Q.front().x,t = Q.front().t; Q.pop();for (int i = 0; i < v[x].size(); i++){int to = v[x][i];if (dis[to][t^1] != INF) continue;dis[to][t^1] = dis[x][t] + 1; Q.push(Point(to,t^1));}}for (int i = n; i >= k; i--) G[k].push_back(data(dis[i][0],dis[i][1]));}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifn = getint(); m = getint(); q = getint();while (m--){int x = getint(),y = getint();v[x].push_back(y); ++du[x];v[y].push_back(x); ++du[y];}for (int i = 1; i <= n; i++) BFS(i);while (q--){int x = getint(),y = getint(),z = getint();if (x == y && z && !du[x]) {puts("NIE"); continue;}if (x > y) swap(x,y);int g = (z & 1) ? G[x][n - y].B : G[x][n - y].A;if (g != INF && g <= z) puts("TAK"); else puts("NIE");}return 0;}

0 0
原创粉丝点击