码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 1519 过路费

时间:2016-01-01 12:52:18      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

 这个和货车运输没有区别。。。只不过换成了最小生成树。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxv 40005
#define maxe 200005
#define maxq 30005
using namespace std;
int n,m,x,y,z,anc[maxv][20],ma[maxv][20],father[maxv],g[maxv],h[maxv];
int nume=0,numt=0,q;
bool vis[maxv],judge[maxv];
int dis[maxv];
struct edge
{
int u,v,w,nxt;
}e[maxe],tr[maxe];
void addedge(int u,int v,int w)
{
e[++nume].u=u;
e[nume].v=v;
e[nume].w=w;
e[nume].nxt=g[u];
g[u]=nume;
}
void addtree(int u,int v,int w)
{
tr[++numt].u=u;
tr[numt].v=v;
tr[numt].w=w;
tr[numt].nxt=h[u];
h[u]=numt;
}
bool cmp(edge x,edge y)
{
return x.w<y.w;
}
int getfather(int x)
{
if (x!=father[x])
father[x]=getfather(father[x]);
return father[x];
}
void unionn(int x,int y)
{
int r1=getfather(x),r2=getfather(y);
father[r1]=r2;
}
void kruskal()
{
for (int i=1;i<=n;i++)
father[i]=i;
for (int i=1;i<=nume*2;i++)
{
int u=e[i].u,v=e[i].v;
if (getfather(u)!=getfather(v))
{
addtree(u,v,e[i].w);
addtree(v,u,e[i].w);
unionn(u,v);
judge[u]=true;
judge[v]=true;
}
}
}
void dfs(int u)
{
vis[u]=true;
for (int i=h[u];i;i=tr[i].nxt)
{
int v=tr[i].v;
if (vis[v]==false)
{
dis[v]=dis[u]+1;
anc[v][0]=u;
ma[v][0]=tr[i].w;
dfs(v);
}
}
}
int lca(int x,int y)
{
if (dis[x]>dis[y]) swap(x,y);
for (int i=15;i>=0;i--)
{
if ((dis[anc[y][i]]>=dis[x]) && (anc[y][i]!=0))
y=anc[y][i];
}
for (int i=15;i>=0;i--)
{
if (anc[x][i]!=anc[y][i])
{
x=anc[x][i];
y=anc[y][i];
}
}
if (x==y) return x;
else return anc[x][0];
}
void work()
{
scanf("%d%d",&x,&y);
if ((judge[x]==false) || (judge[y]==false) || getfather(x)!=getfather(y))
printf("0\n");
else
{
int r=lca(x,y);
int maxx=0;
if (x!=r)
{
for (int i=15;i>=0;i--)
{
if ((dis[anc[x][i]]>=dis[r]) && (anc[x][i]!=0))
{
maxx=max(maxx,ma[x][i]);
x=anc[x][i];
}
}
}
if (y!=r)
{
for (int i=15;i>=0;i--)
{
if ((dis[anc[y][i]]>=dis[r]) && (anc[y][i]!=0))
{
maxx=max(maxx,ma[y][i]);
y=anc[y][i];
}
}
}
if (maxx==0) printf("0\n");
else printf("%d\n",maxx);
}
}
int main()
{
memset(g,0,sizeof(g));
memset(h,0,sizeof(h));
memset(vis,false,sizeof(vis));
memset(dis,0,sizeof(dis));
memset(judge,false,sizeof(judge));
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z);
addedge(y,x,z);
}
sort(e+1,e+2*m+1,cmp);
kruskal();
dfs(1);
for (int e=1;e<=15;e++)
for (int i=1;i<=n;i++)
{
anc[i][e]=anc[anc[i][e-1]][e-1];
ma[i][e]=max(ma[anc[i][e-1]][e-1],ma[i][e-1]);
}
scanf("%d",&q);
for (int i=1;i<=q;i++)
work();
return 0;
}

codevs 1519 过路费

标签:

原文地址:http://www.cnblogs.com/ziliuziliu/p/5093094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!