UVALive 3887 边权极差最小生成树模板
2018-02-02 21:08
411 查看
距期末考还剩两天 GG
这类题要枚举图上的最小边 然后求最小生成树
想一下,最小边之后剩下的边要有n-1-1条 图才能连通 所以下标从1开始的时候枚举到m-n-2就行 取等、
vjudge传送门
要注意一些变量每次循环要初始化
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=201;
const int maxm=20001;
struct edge{
int u,v,w;
}e[maxm<<1];
//枚举最小边 求最小生成树
int n,m,fa[maxn],eid=0;
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
void merge(int a,int b){
a=get(a),b=get(b);
if(a!=b) {fa[a]=b;}
}
bool cmp(edge a,edge b){return a.w<b.w;}
inline int read(){
int s=0,f=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-') f=-1;
c=getchar();
}
while(isdigit(c)){
s=s*10+c-48;
c=getchar();
}
return s*f;
}
int res=99999999;
void init(){
for(int i=1;i<=n;i++) fa[i]=i;
//maxw=-1;
}
int main(){
while(scanf("%d%d",&n,&m)==2&&n){
int a,b,c;
eid=0,res=99999999;
for(int i=1;i<=m;i++)
{
a=read();b=read();c=read();
e[++eid].u=a;e[eid].v=b;e[eid].w=c;
}
sort(e+1,e+m+1,cmp);
// bool flag=true;
for(int k=1;k<=m-n+2;k++)//枚举最小边 求最小生成树
{
init();
int rst=n;
for(int i=k;i<=m;i++) //不必重复排序
{
int x=e[i].u;int y=e[i].v;
if(get(x)!=get(y)){
merge(x,y);
rst--;
}
//printf("%d %d\n",rst,res);
if(rst==1){ //n-1条边 图连通
res=min(e[i].w-e[k].w,res);
break;
}
}
}
if(res!=99999999) printf("%d\n",res);
else printf("-1\n");
}
return 0;
}
这类题要枚举图上的最小边 然后求最小生成树
想一下,最小边之后剩下的边要有n-1-1条 图才能连通 所以下标从1开始的时候枚举到m-n-2就行 取等、
vjudge传送门
要注意一些变量每次循环要初始化
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=201;
const int maxm=20001;
struct edge{
int u,v,w;
}e[maxm<<1];
//枚举最小边 求最小生成树
int n,m,fa[maxn],eid=0;
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
void merge(int a,int b){
a=get(a),b=get(b);
if(a!=b) {fa[a]=b;}
}
bool cmp(edge a,edge b){return a.w<b.w;}
inline int read(){
int s=0,f=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-') f=-1;
c=getchar();
}
while(isdigit(c)){
s=s*10+c-48;
c=getchar();
}
return s*f;
}
int res=99999999;
void init(){
for(int i=1;i<=n;i++) fa[i]=i;
//maxw=-1;
}
int main(){
while(scanf("%d%d",&n,&m)==2&&n){
int a,b,c;
eid=0,res=99999999;
for(int i=1;i<=m;i++)
{
a=read();b=read();c=read();
e[++eid].u=a;e[eid].v=b;e[eid].w=c;
}
sort(e+1,e+m+1,cmp);
// bool flag=true;
for(int k=1;k<=m-n+2;k++)//枚举最小边 求最小生成树
{
init();
int rst=n;
for(int i=k;i<=m;i++) //不必重复排序
{
int x=e[i].u;int y=e[i].v;
if(get(x)!=get(y)){
merge(x,y);
rst--;
}
//printf("%d %d\n",rst,res);
if(rst==1){ //n-1条边 图连通
res=min(e[i].w-e[k].w,res);
break;
}
}
}
if(res!=99999999) printf("%d\n",res);
else printf("-1\n");
}
return 0;
}
相关文章推荐
- 【UVALive】3887 Slim Span 枚举+最小生成树
- UVALive 6437 Power Plant 【最小生成树 + 思维】
- UVALive 3662 Another Minimum Spanning Tree 曼哈顿最小距离生成树
- Another Minimum Spanning Tree - UVaLive 3662 曼哈顿最小生成树
- UVALive 3662 Another Minimum Spanning Tree [离散化+线段树+最小生成树]
- UVA Live 6437 Power Plant 最小生成树
- UVA Live 6437 Power Plant 最小生成树
- UVALive 3662 Another Minimum Spanning Tree 曼哈顿最小生成树
- 曼哈顿最小距离生成树(poj 3241&& UVALive 3662)
- UVALive-7303 Aquarium (最小生成树)
- UVAlive 3662 Another Minimum Spanning Tree 莫队算法,曼哈顿最小生成树
- UVALive 6837 (最小生成树)
- B - Bus Problem UVALive - 7001 最小生成树
- UVa 10034 - Freckles (最小生成树模板题)
- UVALive - 4872 最小生成树
- 【UVALive】5713 Qin Shi Huang's National Road System 最小生成树
- 最小生成树应用uvalive5713
- UVALive 4872 Underground Cables 最小生成树
- UVALive - 5713 最小生成树
- UVALive 7303 Aquarium 最小生成树