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 ?aw:
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 ?xed 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 · 104 ), m (0 ≤ m ≤ 2.5 · 105 ) 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 · 103 ) describing that there is a connection from station a to station b of length c.
The ?rst 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 ?rst 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;
}

原文地址:https://www.cnblogs.com/Kissheart/p/9745990.html

时间: 2024-10-20 07:40:34

upc 2784 Model Railroad的相关文章

hoj 13792 Model Railroad

// 想了半天才发现是道生成树 然后想了好久 出了好多数据都他吗过了 但是a不了 后来发现给定数据 有的就不能构成一棵树 这点没有特判 如果我代码写的好一些 吧生成树写成函数 就他妈规避了这个问题草#include <iostream> #include <algorithm> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #incl

国外火车模型杂志列表

1:64 Modeling Guide - Quarterly publication catering to the S scale model railroader Acadia Collectibles - Online store specializing in model railroad magazine backissues Atlantic Northeast Rails & Ports - Newsletter about the railroads, ports, inter

[Attila GPU] Attila OGL2/D3D9 GPU C Model Simulator

http://www.opengpu.org/forum.php?mod=viewthread&tid=1094&highlight=Attila 查看: 4979|回复: 14    [Attila GPU] Attila OGL2/D3D9 GPU C Model Simulator [复制链接]     ic.expert 管理员 注册时间 2007-7-11 积分 32646 串个门 加好友 打招呼 发消息 电梯直达 1#  发表于 2009-10-19 01:29:41 |只看该

cs224d problem set2 (三) 用RNNLM模型实现Language Model,来预测下一个单词的出现

今天将的还是cs224d 的problem set2 的第三部分习题, 原来国外大学的系统难度真的如此之大,相比之下还是默默地再天朝继续搬砖吧 (注意前方高能,一大批天书即将来袭) ''' Created on 2017年9月26日 @author: weizhen ''' import getpass import sys import time import numpy as np from copy import deepcopy from utils import calculate_p

Data Model for Message Receiver

1. Physical Data Model 2. SQL Statements drop database MessageReceiver go /*==============================================================*/ /* Database: MessageReceiver */ /*==============================================================*/ create dat

springMVC:modelandview,model,controller,参数传递

转载:http://blog.csdn.net/wm5920/article/details/8173480 1.web.xml 配置: copy <> ></> ></> > >> ></> ></> > ></> </> <> ></> ></> </> 这样,所有的.htm的请求,都会被Dispatche

NHibernate框架与BLL+DAL+Model+Controller+UI 多层架构十分相似--『Spring.NET+NHibernate+泛型』概述、知识准备及介绍(一)

原文://http://blog.csdn.net/wb09100310/article/details/47271555 1. 概述 搭建了Spring.NET+NHibernate的一个数据查询系统.之前没用过这两个框架,也算是先学现买,在做完设计之 后花了一周搭建成功了.其中,还加上了我的一些改进思想,把DAO和BLL之中相似且常用的增删改查通过泛型T抽象到了DAO和BLL的父类中,其DAO 和BLL子类只需继承父类就拥有了这些方法.和之前的一个数据库表(视图)对应一个实体,一个实体对应一

asp.net mvc 提交model 接收不了

[HttpPost]        //[ValidateInput(false)]        public ActionResult AddNews1(_54Young_News_Model.model.gou54contentall contentmodel, _54Young_News_Model.model.gou54user usermodel)        {} 发现用一些特殊符号提交不了, 然后以为说前端问题,把model去掉就可以了. 后面觉得是因为有特殊符号影响到转mod

Django操作model时,报错:AttributeError:’ProgrammingError’ object has no attribute ‘__traceback__’

原因:在Django项目下相应的应用下面的models.py配置的model(也就是class)没有创建成相应的表. 这是怎么回事呢? 首先,将models.py里面的model创建成对应的数据库表的执行命令(DOS命令)为:manage.py syncdb. 但是我自己的电脑上执行该命令时,显示.Unknown command:syncdb.执行,manage.py help后的确没有发现这个子命令.最后网上搜索发现这个命令已经在Django1.9里面取消了.并且stackoverflow里面