「BZOJ3417」POI2013 Tales of seafaring
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.
一个n点m边无向图,边权均为1,有k个询问
每次询问给出(s,t,d),要求回答是否存在一条从s到t的路径,长度为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
题解
http://wenku.baidu.com/link?url=Xsi-1wIc3hQXZLNQx-KVI9bMOgq5HA3VpgG7NbnktJsGDK6Mx3lLXGXJ9lVO5p6nhlWk_zXExPB7kPd1-ek8kiDTOPfVJFoa2rkN5zsThkC
为了防止爆内存可以把询问排序,x相同的分为一块bfs(x)然后记录答案
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 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<queue> #include<cmath> #include<vector> #include<algorithm> #include<map> #define inf 1000000000 #define ll long long using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,K,cnt; int last[5005],d[5005][2]; int q[10005],f[10005],ans[1000005]; bool lk[5005]; struct data{int to,next;}e[10005]; struct query{int x,y,d,id;}que[1000005]; bool operator<(query a,query b) { return a.x<b.x; } void insert(int u,int v) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; } void bfs(int x) { memset(d,-1,sizeof(d)); int head=0,tail=1; q[0]=x;f[0]=0;d[x][0]=0; while(head!=tail) { int now=q[head],flag=f[head];head++; int to=flag^1; for(int i=last[now];i;i=e[i].next) { if(d[e[i].to][to]==-1) { d[e[i].to][to]=d[now][flag]+1; q[tail]=e[i].to;f[tail]=to;tail++; } } } } int main() { n=read();m=read();K=read(); for(int i=1;i<=m;i++) { int u=read(),v=read(); insert(u,v); insert(v,u); lk[u]=lk[v]=1; } for(int i=1;i<=K;i++) que[i].id=i,que[i].x=read(),que[i].y=read(),que[i].d=read(); sort(que+1,que+K+1); int now=1; for(int k=1;k<=K;k=now+1,now=k) { while(que[now+1].x==que[k].x&&now<K)now++; bfs(que[now].x); for(int t=k;t<=now;t++) { int x=que[t].x,y=que[t].y,v=que[t].d,id=que[t].id; if(x==y&&!lk[x])continue; int dis=d[y][v&1]; if(dis!=-1&&v>=dis)ans[id]=1; } } for(int i=1;i<=K;i++) if(ans[i])puts("TAK"); else puts("NIE"); return 0; } |
relógios famosos
Além disso, olhar para alto teor de fibras e proteínas.
long sleeve wedding dress ideas
wow its great post..
giuseppe zanotti mesh booties
hey good job man.
ray ban aviators lenses
nice site, all posts are very nice.
louis vuitton vernis alma handbag
Thank you admins.
ray ban 4147 polarized replacement lenses
thank you
apple green coach purse
Very good, i like you.And say and say ¡¡¡. It’s greatttttttttttt¡¡..
giuseppe zanotti mary jane
thanks, nice post.
gothic wedding dresses corset
Good job!
ray ban wayfarer tortoise fake
this is appreciable.!!!!!!i like it.
wedding dress patterns for plus size
Thank you!!!!
giuseppe zanotti yellow shoes rosemere
Nice posts indeed
coach book bags on sale
Valuable thoughts and advices. I read your topic with great interest.
louis vuitton man bag price uk
Just what I needed, thanks a lot.
nike air max 90 grey and yellow
Great post.Thanks a lot.
herve leger elbise istanbul
wonderful post, thank you.
giuseppe zanotti shoes run small business
Thanks for such a great post and the review, I am totally impressed!
ray ban 4068 grau
nice and thanks.
louis vuitton never full bag on sale
this is appreciable.!!!!!!i like it.
louis vuitton watches for sale
thank you
coach bag chelsea tote
a very successful site. Also very revealing article. Thanks to the contributors.
giuseppe zanotti round toe pumps uk
this is just perfect!
louis vuitton handbags grey and white
Such a good article, caught my sympathy!