您的位置:首页 > 其它

hdu 3938 Portal

2016-02-09 00:43 381 查看
对于新增加的每一条边,ab,假如a点和b点原来不连通,那么新增加路径的条数(包含ab的路径)=原来和a点连通的点数(包括自身)*原来和b点连通的点数(包括自身)。

假如a和b已经连通,那么不增加。(为什么不知道,凑数据,我当时题目一直觉得没看懂,因为题目说的非常含糊)

这份题解:写的很清楚

http://blog.csdn.net/sdj222555/article/details/7439187

按照边权值从小到大的顺序给编排序,记录前i条边可以组成的路径数量。

查询时我使用了二分。


Portal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1250    Accepted Submission(s): 616


Problem Description

ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length
of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.

 

Input

There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains
three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).

 

Output

Output the answer to each query on a separate line.

 

Sample Input

10 10 10
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6

 

Sample Output

36
13
1
13
36
1
36
2
16
13

 

Source

2011 Multi-University Training Contest 10 -
Host by HRBEU

 

/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:hdu 3938
* @type: 并查集
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 10000 ;
const int maxm= 50000 ;
int n,m,q;
int pre[maxn+5];
int a[maxn+5];
struct Edge
{
int x,y,w;
Edge(){}
Edge(int x,int y,int w):x(x),y(y),w(w){}
bool operator<(const Edge t)const
{
return w<t.w;
}

}edges[maxm+5];

ll dp[maxm+5];

int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
}

void work()
{
dp[0]=0;
int s;
for(int i=1;i<=m;i++)
{
dp[i]=dp[i-1];
int x=edges[i].x;
int y=edges[i].y;
int rx=find(x);
int ry=find(y);
if(rx==ry) continue;
dp[i]+= a[rx]*a[ry];
pre[rx]=ry;
a[ry]+=a[rx];

}
}

void init()
{
for(int i=1;i<=n;i++) pre[i]=i,a[i]=1;
}

void merge(int x,int y)
{
int rx=find(x);
int ry=find(y);
if(rx==ry) return;
pre[rx]=ry;
}

int bs(int le,int ri,int x)
{

if(edges[ri].w<x) return ri;
while(le<=ri)
{
int mid=(le+ri)>>1;
if(edges[mid].w<=x) le=mid+1;
else ri=mid-1;
}
return ri;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
int x,y,w;
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
edges[i]=Edge(x,y,w);
}
sort(edges+1,edges+1+m);
work();
for(int i=1;i<=q;i++)
{
scanf("%d",&x);
int p=bs(1,m,x);
// cout<<p<<endl;
printf("%lld\n",dp[p]);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: