bzoj3417:[POI2013]MOR-Tales of seafaring

传送门

这个题比较水,很容易看出
1、最短路小于d,直接看奇偶性就好了
2,最短路大于d,puts("NIE\n");
主要就是判奇偶性的问题,将每个点拆成奇点和偶点跑bfs就行了
在线需要开short,不然空间会炸,离线就没有这个忧虑
代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=5e3+1;
int n,m,k,ans,pre[maxn*4],nxt[maxn*4],h[maxn*2],cnt;
short g[maxn][maxn*2];
queue<int>q;
void add(int x,int y)
{
	pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt;
	pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt;
}
void bfs(int y)
{
	q.push(y);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(rg int i=h[x];i;i=nxt[i])
			if(!g[y][pre[i]])g[y][pre[i]]=g[y][x]+1,q.push(pre[i]);
	}
}
int main()
{
	read(n),read(m),read(k);
	for(rg int i=1,x,y;i<=m;i++)read(x),read(y),add(x,y+n),add(x+n,y);
	for(rg int i=1;i<=n;i++)bfs(i);
	for(rg int i=1,x,y,z;i<=k;i++)
	{
		read(x),read(y),read(z);
		if(z&1)(g[x][y+n]&&g[x][y+n]<=z)?printf("TAK\n"):printf("NIE\n");
		else (g[x][y]&&g[x][y]<=z)?printf("TAK\n"):printf("NIE\n");
	}
}
posted @ 2019-02-14 16:42  蒟蒻--lichenxi  阅读(190)  评论(0编辑  收藏  举报