BZOJ 2435(BFS+输入输出外挂)

2435: [Noi2011]道路修建

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 2299   Solved: 682
[ Submit][ Status][ Discuss]

Description

在 W 星球上有 n 个国家。为了各自国家的经济发展,他们决定在各个国家
之间建设双向道路使得国家之间连通。但是每个国家的国王都很吝啬,他们只愿
意修建恰好 n – 1条双向道路。 每条道路的修建都要付出一定的费用, 这个费用等于道路长度乘以道路两端的国家个数之差的绝对值。例如,在下图中,虚线所示道路两端分别有 2 个、4个国家,如果该道路长度为 1,则费用为1×|2 – 4|=2。图中圆圈里的数字表示国家的编号。


由于国家的数量十分庞大,道路的建造方案有很多种,同时每种方案的修建
费用难以用人工计算,国王们决定找人设计一个软件,对于给定的建造方案,计
算出所需要的费用。请你帮助国王们设计一个这样的软件。

Input


输入的第一行包含一个整数n,表示 W 星球上的国家的数量,国家从 1到n
编号。接下来 n – 1行描述道路建设情况,其中第 i 行包含三个整数ai、bi和ci,表
示第i 条双向道路修建在 ai与bi两个国家之间,长度为ci。


Output

输出一个整数,表示修建所有道路所需要的总费用。

Sample Input


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

Sample Output


20

HINT



n = 1,000,000 1≤ai, bi≤n 

0 ≤ci≤ 10^6

算法描述:此题乍一看DFS;实际上 从数据量看100W的数据 DFS肯定会爆栈;因为DFS一般是函数递归形式调用;每次函数递归都要进栈;数据量大了,就会爆栈;

所以BFS解决! 不过我在写的时候直接上BFS 也WR了!后来看大神的解题代码!发现加上输入输出外挂后立马 AC!

/**********************************************

本题必须用到输入输出外挂
****************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 1001000
typedef long long LL;
struct Node {
    int value,fa,s,next;
    Node(){}
    Node(int _v,int _next,int _fa,int _s){
        value = _v; next = _next;
        fa = _fa;  s = _s;
    }
}node[N<<1],stack[N<<1];
queue<Node> q;
LL Abs(LL a){  return a >= 0? a : -a; }
int head[N<<1],top,n,siz[N];
void add_Node(int a,int b,int value) {
    node[top].fa = a;
    node[top].s = b;
    node[top].value = value;
    node[top].next = head[a];
    head[a] = top++;
}
void bfs(int root,LL &ans) {
    while(!q.empty()) q.pop();
    int top_stack = 0;
    q.push(Node(0,-1,-1,root));
    while(!q.empty()) {
        Node cur = q.front(); q.pop();
        stack[top_stack++] = Node(cur.value,cur.next,cur.fa,cur.s);
        for(int i = head[cur.s];i!=-1;i = node[i].next){
            if(cur.fa == node[i].s) continue;
            q.push(Node(node[i].value,node[i].next,node[i].fa,node[i].s));
        }
    }
    for(int i = top_stack-1;i>=0;i--){
        if(stack[i].fa != -1) siz[stack[i].fa] += siz[stack[i].s];
        ans += stack[i].value*Abs(2*siz[stack[i].s] - n);
    }
}
void init(){
    memset(head,-1,sizeof(head));
    for(int i = 1;i<=n;i++) siz[i] = 1;
    top = 0;
}
inline LL get(){
    LL ret = 0;
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)){
        ret = ret*10+(LL)(c - '0');
        c = getchar();
    }
    return ret;
}
inline void out(LL x){
    if(x > 9) out(x/10);
    putchar(x%10+'0');
}
int main(){
    int a,b,c;
    //freopen("road20.in","r",stdin);
    n = get();
    init();
    for(int i = 1;i<n;i++){
        a = get(); b = get(); c = get();
        add_Node(a,b,c);
        add_Node(b,a,c);
    }
    LL ans = 0;
    bfs(1,ans);
    out(ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值