【NOIP2013-D1T3】货车运输
Time Limit:10000MS Memory Limit:128000K
Total Submit:66 Accepted:42
Case Time Limit:1000MS
Description
A国有n座城市,编号从1到n,城市之间有m条双向道路。
每一条道路对车辆都有重量限制,简称限重。
现在有q辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。
Input
第一行有两个用一个空格隔开的整数 n,m,表示 A 国有n座城市和m条道路。
接下来m行,每行3个整数x,y,z,表示从x号城市到y号城市有一条限重为z的道路(x≠y,两座城市之间可能有多条道路)
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来q行,每行两个整数x,y,之间用一个空格隔开,表示一辆货车需要从x城市运输货物到y城市(x≠y)。
Output
输出共有 q 行,每行一个整数,表示对于这一辆货车,它的最大载重是多少。
如果货车不能到达目的地,输出-1。
Sample Input
4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
Sample Output
3
-1
3
Hint
对于 30%的数据,1<=n<=1000,1<=m<=10000,1<=q<=1000;
对于 60%的数据,1<=n<=1000,1<=m<=50000,1<=q<=1000;
对于 100%的数据,1<=n<=10000,1<=m<=50000,1<=q<=30000,0<=z<=100000。
MST+LCA 裸题,但是需要开一个t点连接每个点,权值为-1。
然后看了nodgd的证明,然后就不动脑子了。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 10009
#define maxm 70009
#define inf 10000000
struct node{int from, to, len;}a[maxm];
int father[maxn];
int need[maxn], dep[maxn], fa[maxn][20], mmin[maxn][20];
bool wasfa[maxn];
vector<pair<int, int> >edge[maxn];
inline void _read(int& d)
{
char t=getchar();bool f=false;
while(t<'0'||t>'9') {if(t=='-') f=true; t=getchar();}
for(d=0;t<='9'&&t>='0';t=getchar()) d=d*10+t-'0';
if(f) d=-d;
}
inline void _out(int d)
{
int o[30],top=1;
if(d==0){putchar('0');return ;}
if(d<0) {putchar('-');d=-d;}
while(d)
{
o[top++]=d%10;
d/=10;
}
for(--top;top;--top) putchar('0'+o[top]);
}
void dfs(int cur)
{
int s, i, son;
dep[cur]=dep[fa[cur][0]]+1;
s=ceil(log(dep[cur])/log(2));
for(i=1; i<=s; i++)
{
fa[cur][i]=fa[fa[cur][i-1]][i-1];
mmin[cur][i]=min(mmin[cur][i-1], mmin[fa[cur][i-1]][i-1]);
}
wasfa[cur]=true;
vector<pair<int, int> >::iterator it;
for(it=edge[cur].begin(); it!=edge[cur].end(); it++)
{
son=it->first;
if(!wasfa[son])
{
fa[son][0]=cur;
mmin[son][0]=it->second;
dfs(son);
}
}
}
int lca(int x, int y)
{
int k, s, i, ans=inf;
if(dep[x]<dep[y]) swap(x, y);
k=dep[x]-dep[y];
s=ceil(log(dep[x])/log(2));
for(i=0; i<=s; i++)
{
if(k&(1<<i))
{
ans=min(ans, mmin[x][i]);
x=fa[x][i];
}
}
if(x==y) return ans;
s=ceil(log(dep[x])/log(2));
for(i=s; i>=0; i--)
{
if(fa[x][i]!=fa[y][i])
{
ans=min(ans, mmin[x][i]);
x=fa[x][i];
ans=min(ans, mmin[y][i]);
y=fa[y][i];
}
}
ans=min(ans, mmin[x][0]);
ans=min(ans, mmin[y][0]);
return ans;
}
bool cmp(node a, node b)
{
if(a.len>b.len) return 1;
return 0;
}
int getfather(int x)
{
if(x!=father[x])father[x]=getfather(father[x]);
return father[x];
}
void kruskal(int n)
{
int x, y, k=0, cnt=1, tot;
while(cnt<n)
{
k++;
x=getfather(a[k].from);
y=getfather(a[k].to);
if(x!=y)
{
father[x]=y;
need[cnt]=k;
cnt++;
}
}
}
int et=0;
void adde(int f, int t, int l)
{
et++;
a[et].from=f;
a[et].to=t;
a[et].len=l;
}
int main()
{
int u, v, l;
int n, m, i, x, y, z, q;
scanf("%d%d", &n, &m);
for(i=1; i<=n+3; i++)
{
father[i]=i;
}
for(i=1; i<=m; i++)
{
_read(x);
_read(y);
_read(z);
adde(x, y, z);
}
for(i=1; i<=n; i++)
{
u=n+1;
v=i;
l=-1;
adde(u, v, l);
}
n++;
sort(&a[1], &a[et+1], cmp);
kruskal(n);
for(i=1; i<n; i++)
{
u=a[need[i]].from;
v=a[need[i]].to;
l=a[need[i]].len;
edge[u].push_back(make_pair<int, int>(v, l));
edge[v].push_back(make_pair<int, int>(u, l));
}
dfs(1);
scanf("%d", &q);
for(i=1; i<=q; i++)
{
_read(x);
_read(y);
_out(lca(x, y));
putchar('\n');
}
return 0;
}