upc 2784 Model Railroad

 

Model Railroad

时间限制: 1 Sec  内存限制: 64 MB
提交: 173  解决: 45
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Since childhood you have been fascinated by model railroads. Designing your own tracks,complicated intersections, train stations with miniatures of travellers, train operators, luggage is so much fun! However, it also needs a lot of space. Since your house is more than full by now, you decide to move to the garden.
You have already moved all your completed tracks outside when you notice an important flaw:
Since different tracks were in different rooms before, there are stations which cannot be reached from each other. That has to change!
Since you have already fixed the exact positions of the stations, you know the lengths for all possible connections you can build and also which stations are connected already. All connections can be used in both directions. You can decide to remove some existing connections and instead build new ones of at most the same total length. Is it possible to rearrange the railroads so that every station is reachable from all other stations?

输入

The input consists of:
• one line with three integers n (2 ≤ n ≤ 5 · 10 4 ), m (0 ≤ m ≤ 2.5 · 10 5 ) and l (0 ≤ l ≤ m),where n is the number of stations, m is the number of possible connections and l is the number of connections already built;
• m lines describing the connections. Each connection is described by:
– one line with three integers a, b (1 ≤ a, b ≤ n), and c (0 ≤ c ≤ 5 · 10 3 ) describing that there is a connection from station a to station b of length c.
The first l of those connections already exist.

输出

Output “possible” if it is possible to construct a connected network as described above,otherwise output “impossible”.

样例输入

4 6 3
1 2 1
2 1 2
3 4 2
1 3 2
1 4 3
2 4 2

样例输出

possible

提示

Figure E.1 depicts the first sample case. It is possible to connect all stations by removing the connections between stations 1 and 2 of length 2 and instead building the connection between stations 2 and 4. The curvature of the rails does not matter because you have a hammer. In the second case, depicted in Figure E.2, it is not possible to connect all three stations.

题意

n个点,m条边,其中前i条边已经被建好,已知每条边的长度,要想修建一条没建好的边必须拆一条比待建边长度短的一条已经建好的边。最终要使得每个点相互连通。

分析

跑一边最小生成树,判断一下最小生成树的代价是否比前i条边的代价小,最后再判断一下是否联通,即看一下最小生成树是不是n-1条边就行了。

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=3e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
struct edge
{
    int x,y,w;
    edge(int x=0,int y=0,int w=0):x(x),y(y),w(w){}
}e[MAX];

int fa[MAX],n,m;
int p,sum1,sum2;


int getfather(int x)
{
    if(x==fa[x]) return x;
    else return fa[x]=getfather(fa[x]);
}

int cmp(edge a,edge b)
{
    return a.w<b.w;
}
int cur=0;
int kruscal()
{
    int ans=0;
    sort(e+1,e+1+m,cmp);
    int cnt=n;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        int t1=getfather(e[i].x);
        int t2=getfather(e[i].y);
        if(t1!=t2)
        {
            fa[t1]=t2;
            ans+=e[i].w;
            cur++;
            if(cnt==1) break;
        }
    }
    return ans;
}
int main()
{
    scanf("%d%d%d",&n,&m,&p);

    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
        if(i<=p) sum1+=e[i].w;
    }

    sum2=kruscal();


    
    if(cur!=n-1)
    {
        printf("impossible\n");
        return 0;
    }
    
    if(sum2<=sum1)
        printf("possible\n");
    else
        printf("impossible\n");

    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/Kissheart/p/9745990.html
UPC