1 条题解
-
0
C++ :
#include <cstdio> #include <cstring> #include <climits> #include <algorithm> #include <cctype> #include <cmath> using namespace std; int N=0,M=0,r1=0,r2=0; class node{ public: int s,t,v; bool operator < (const node& b) const { return v>b.v; } }E[100010]; int F[100000]={0}; inline int find(int x){ if(F[x]!=x) F[x]=find(F[x]); return F[x]; } inline void Union(int x,int y){ r1=find(x); r2=find(y); if(r1!=r2) F[r1]=r2; } int main(){ //freopen("prison.in","r",stdin); //freopen("prison.out","w",stdout); scanf("%d%d",&N,&M); for(int i=1;i<=2*N+2;++i) F[i]=i; for(int i=1;i<=M;++i){ scanf("%d%d%d",&E[i].s,&E[i].t,&E[i].v); } sort(E+1,E+M+1); for(int i=1;i<=M;++i){ Union(E[i].s,E[i].t+N); Union(E[i].t,E[i].s+N); if(find(E[i].s)==find(E[i].s+N) || find(E[i].t)==find(E[i].t+N) ){ printf("%d\n",E[i].v); fclose(stdin); fclose(stdout); return 0; } } printf("0\n"); fclose(stdin); fclose(stdout); return 0; } /*4 6 1 4 2533 2 3 3511 1 2 28351 1 3 6617 2 4 1805 3 4 12883*/Pascal :
type bian=record x,y,d:longint; end; //============================================================================== var f:Array[0..20000] of longint; h:array[0..20000] of longint; map:array[0..100000] of bian; n,m:longint; //============================================================================== procedure qsort(l,r:longint); var i,j,mid:longint; t:bian; begin i:=l; j:=r; mid:=map[(l+r) div 2].d; repeat while map[i].d>mid do inc(i); while map[j].d<mid do dec(j); if i<=j then begin t:=map[i]; map[i]:=map[j]; map[j]:=t; inc(i); dec(j); end; until i>j; if i<r then qsort(i,r); if l<j then qsort(l,j); end; //============================================================================== procedure init; var i:longint; begin readln(n,m); fillchar(h,sizeof(h),0); for i:=1 to m do readln(map[i].x,map[i].y,map[i].d); for i:=1 to n do f[i]:=i; qsort(1,m); // for i:=1 to m do // writeln(map[i].d); end; //============================================================================== function get(t:longint):longint; begin if f[t]=t then exit(t); f[t]:=get(f[t]); exit(f[t]); end; //============================================================================== procedure union(t1,t2:longint); var xx,yy:longint; begin xx:=get(t1); yy:=get(t2); if xx<>yy then f[xx]:=yy; end; //============================================================================== procedure main; var tx,ty,i,j:longint; begin for i:=1 to m do begin tx:=get(map[i].x); ty:=get(map[i].y); if tx=ty then{如果同为一个监狱(并查集)} begin writeln(map[i].d); close(input); close(output); halt; end; if (h[map[i].x]=0) and (h[map[i].y]=0) then{如果都没有敌人就互为敌人} begin h[map[i].x]:=map[i].y; h[map[i].y]:=map[i].x; continue; end; if h[map[i].x]=0 then{如果x没敌人,就把y当x的敌人} begin h[map[i].x]:=map[i].y; end; if h[map[i].y]=0 then{如果y没敌人,就把x当y的敌人} begin h[map[i].y]:=map[i].x; end; union(h[map[i].x],map[i].y);{合并两个并查集} union(h[map[i].y],map[i].x); end; writeln(0); end; //============================================================================== begin // assign(input,'prison.in'); reset(input); //assign(output,'prison.out'); rewrite(output); init; main; //close(input); close(output); end.
- 1
信息
- ID
- 2587
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者