现在的位置: 首页 > 综合 > 正文

最小生成树不得不说的一题

2013年06月01日 ⁄ 综合 ⁄ 共 3438字 ⁄ 字号 评论关闭

这题又做了大半天。。题目如下

Description

Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.

 

The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to
city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.

 

More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is
defined as the maximum dangerousness of any road on this path.

 

Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.

 

 

Input

There will be at most 5 cases in the input file.

The first line of each case contains two integers NM (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe
the roads. The i-th of these lines contains three integers: xiyidi (1 ≤ xiyi ≤ N,
0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.

 

Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si andti (1
≤ siti  ≤ Nsi != ti).

 

Consecutive input sets are separated by a blank line.

 

Output

For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output
blocks are separated by a blank line.

 

The input file will be such that there will always be at least one valid path.

这题显然先建一个最小生成树,接着就开始遍历,寻点,但直接做的话会超时= =!所以坑爹了……

AC的一种做法是把最小生成树转化为一个有根数,然后每次由两个目标点分别寻根,

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
struct Edge
{
    int a,b;
    int len;
}edge[900000];
struct Tree
{
    int from,to;
    int next;
    int len;
}tree[900000];

int N,M,T,num,pmin;
int list[50005],vis[50005],parent[50005],flag[50005],fa[50005][2],d1[50005],d2[50005];

int root( int p )
{
	if( parent[p] == -1 ) return p;
	else return parent[p] = root( parent[p] );
}

void merge( int a , int b )
{
	a = root( a );
	b = root( b );
	parent[a] = b;
}


void dfs1(int x)      //让 a 找到 1 ,寻求沿路的最大值
{
    int i=x;
    while(i)
    {
        vis[i]=1;
        if(fa[i][1] < d1[i]) d1[fa[i][0]]=d1[i];
        else d1[fa[i][0]]=fa[i][1];
        i=fa[i][0];
    }
}

int dfs(int x)  // 让 b 找 1 ,如果途径 a 走过的点就弹出来
{
    int i=x;
    while(i)
    {
        if(fa[i][1] < d2[i]) d2[fa[i][0]]=d2[i];
        else d2[fa[i][0]]=fa[i][1];
        if(vis[i]) return i;
        i=fa[i][0];
    }
    return 1;
}

bool cmp(Edge a,Edge b)
{
    return a.len<b.len;
}
void build_tree()   //建立一个有根树,把 1 作为“根”
{
    queue<int> Q;
    memset(flag,0,sizeof(flag));
    flag[1]=1;
    Q.push(1);
    fa[1][0]=0,fa[1][1]=0;
    while(!Q.empty())
    {
        int temp=Q.front();
        Q.pop();
        for(int i=list[temp];i!=-1;i=tree[i].next)
        {
            if(flag[tree[i].to] == 0)
            {
                Q.push(tree[i].to);
                fa[tree[i].to][0]=temp;
                fa[tree[i].to][1]=tree[i].len;
                flag[tree[i].to]=1;
            }
        }
    }
}
int main()
{
    int i,j,k,a,b,c,nu=0;
    while(scanf("%d %d",&N,&M)!=EOF)
    {
        if(nu++) printf("\n");
        memset(list,-1,sizeof(list));
        memset(parent,-1,sizeof(parent));
        for(i=0;i<M;i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            edge[i].a=a,edge[i].b=b,edge[i].len=c;
        }
        sort(edge,edge+M,cmp);
        for(i=0,num=0,j=0;i<M;i++)
        {
            if(root(edge[i].a)!=root(edge[i].b))
            {
                merge(edge[i].a,edge[i].b);
                tree[num].from=edge[i].a,tree[num].to=edge[i].b,tree[num].len=edge[i].len,tree[num].next=list[edge[i].a],list[edge[i].a]=num++;
                tree[num].from=edge[i].b,tree[num].to=edge[i].a,tree[num].len=edge[i].len,tree[num].next=list[edge[i].b],list[edge[i].b]=num++;
            }
        }  //建立一个最小生成树
        scanf("%d",&T);
        build_tree();
        while(T--)
        {
            scanf("%d %d",&a,&b);
            memset(vis,0,sizeof(vis));
            memset(d1,0,sizeof(d1));
            memset(d2,0,sizeof(d2));
            dfs1(a);
            int t=dfs(b);
            printf("%d\n",d1[t]>d2[t]?d1[t]:d2[t]);
        }
    }
}

抱歉!评论已关闭.