您的位置:首页 > 其它

FJUT 3104 海平面上升 最短路 或者 离线+并查集

2017-11-19 18:27 141 查看
http://120.78.128.11/Problem.jsp?pid=3104


海平面上升

TimeLimit:2500MS  MemoryLimit:128MB

64-bit integer IO format:%lld

已解决 | 点击收藏

Problem Description

有n个岛m座桥。当海水涨潮时是有些道路会被淹没而导致无法通行。
现在已知每座桥的高度和海平面高度。当海平面高度大于等于桥的高度时,这座桥将无法通行。而任意时刻岛屿都不会被淹没,问某些岛之间是否能够通行。
两个岛之间的桥可能不止一座

Input

每个测试文件仅有一组数据
第一行是三个数组n,m,q。分别代表岛屿数量,桥的数量和查询次数
接下来有m行,每行有三个数字 a,b,h代表岛屿a与岛屿b直接有个一座高度为h的桥。
再接下来有q行,每行有三个数字x, y,h。代表查询当海水高度为h时,x,y之间是否能够通行

n<=200,m<=20000,q<=20000
1<=a,b,x,y<=n  且a!=b,x!=y

0<=h<=1e9

Output

如果能够通行请输出"G0",否则"PlTY"
请注意桥是双向通行的并仔细审题



SampleInput

10 9 5
5 7 76
7 1 30
1 9 96
9 6 16
6 3 61
3 2 86
2 8 28
8 10 82
10 4 1
10 9 29
5 9 75
7 3 5
3 10 47
1 6 62


SampleOutput

PlTY
PlTY
G0
PlTY
PlTY


在线做法是为每个点跑个最短路,路的权为所经过的边的中最低的边的高度,且权大的优先。

离线做法就很吊了,先把询问都存下来,然后按高度排序,从大到小排,进行贪心,用并查集合并联通块,
最后看这个u,v是否在同一个连通块内。

/// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
/// __.' ~. .~ `.__
/// .'// \./ \\`.
/// .'// | \\`.
/// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
/// .'//.-" `-. | .-' "-.\\`.
/// .'//______.============-.. \ | / ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scan_d(x)
#define S_2(x,y) scan_d(x),scan_d(y)
#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=205;
const int maxx=1e6+10;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' &&a
13cf6
mp; c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int n,m,q;
int dp[maxn][maxn];
void solve()
{
s_3(n,m,q);
FOR(1,m,i)
{
int a,b,h;
s_3(a,b,h);
dp[a][b]=max(dp[a][b],h);
dp[b][a]=max(dp[b][a],h);
}
FOR(1,n,k)
FOR(1,n,i)
FOR(1,n,j)
dp[i][j]=max(dp[i][j],min(dp[i][k],dp[k][j]));
W(q--)
{
int x,y,h;
s_3(x,y,h);
if(dp[x][y]>h)
puts("G0");
else puts("PlTY");
}
}

int main()
{
//freopen( "1.in" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int t=1;
//init();
//s_1(t);
for(int cas=1;cas<=t;cas++)
{
//printf("Case #%d: ",cas);
solve();
}
}

///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \./                  \\`.
///        .'//                     |                     \\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\`.
///   .'//______.============-..   \ | /   ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scanf("%I64d",&x)
#define S_2(x,y) scanf("%I64d%I64d",&x,&y)
#define S_3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=1e18+10;
//const int dx[]={-1,0,1,0,1,-1,-1,1};
//const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=2e5+10;
const int maxx=2e5+10;
const double EPS=1e-10;
const double eps=1e-10;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
/*
void readString(string &s)
{
static char str[maxx];
scanf("%s", str);
s = str;
}
*/
struct node
{
int u,v,value;
}Q[maxx];

struct node1
{
int u,v,value,id;
}q[maxx];

int fa[maxx];
int fi(int x)
{
return fa[x]==x?x:fa[x]=fi(fa[x]);
}

void unio(int x,int y)
{
int p1=fi(x),p2=fi(y);
if(p1==p2) return ;
fa[p1]=p2;
}

bool cmp(node a,node b)
{
return a.value>b.value;
}

bool cmp1(node1 a,node1 b)
{
return a.value>b.value;
}

bool ans[maxn];

int n,m,t;
void solve()
{
s_3(n,m,t);
FOR(1,n,i) fa[i]=i;
FOR(1,m,i)
{
int u,v,h;
s_3(u,v,h);
Q[i].u=u;Q[i].v=v;Q[i].value=h;
}
sort(Q+1,Q+m+1,cmp);
FOR(1,t,i)
{
int u,v,h;
s_3(u,v,h);
q[i].u=u;q[i].v=v;
q[i].id=i;q[i].value=h;
}
sort(q+1,q+t+1,cmp1);
int cnt=1;
FOR(1,t,i)
{
W(Q[cnt].value>q[i].value&&cnt<=m)
{
unio(Q[cnt].u,Q[cnt].v);
cnt++;
}
ans[q[i].id]=(fi(q[i].u)==fi(q[i].v));
}
FOR(1,t,i)
{
if(ans[i]) puts("G0");
else puts("PlTY");
}
}

int main()
{
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int t=1;
//init();
//s_1(t);
for(int cas=1;cas<=t;cas++)
{
//printf("Case #%d: ",cas);
solve();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: