zoukankan      html  css  js  c++  java
  • 大臣的旅费

    需要注意的是开数据的问题。本题比较坑,没有给出问题规模。但是估计有一万个顶点,我用的前向星,边表需要V²的大小,但是这样开会爆掉。题设给出边的数目不会超过顶点的数目,这很关键,所以直接开成V×2(因为是无向图要两边连)

    还有就是推公式,这个比较简单,通过样例数据看出是等差数列求和即可。

    但是用dfs会超时,最后一组数据过不了

    dfs代码:

    #include <stdio.h>
    #include <memory.h>
    #include <math.h>
    #include <string>
    #include <string.h>
    #include <vector>
    #include <set>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <map>
    
    #define I scanf
    #define OL puts
    #define O printf
    #define F(a,b,c) for(a=b;a<c;a++)
    #define FF(a,b) for(a=0;a<b;a++)
    #define FG(a,b) for(a=b-1;a>=0;a--)
    #define LEN 10010
    #define MAX 0x06FFFFFF
    #define V vector<int>
    
    using namespace std;
    
    
    struct edge{
        int to,w,next;
    }mp[LEN*2];
    int head[LEN];
    int eID=0;
    void add(int u,int v,int w){
        mp[eID].to=v;
        mp[eID].w=w;
        mp[eID].next=head[u];
        head[u]=eID++;
    }
    
    int vis[LEN];
    
    int maxD=0;
    
    void dfs(int s,int d) {
        maxD=max(maxD,d);
        vis[s]=1;
        int i;
        for(i=head[s];~i;i=mp[i].next){
            int to=mp[i].to;
            if(vis[to]==0){
                dfs(to,d+mp[i].w);
            }
        }
        vis[s]=0;
    }
    
    int main(){
    //    freopen("D:/CbWorkspace/blue_bridge/大臣的旅费.txt","r",stdin);
        int N,i,j,a,b,c;
        I("%d",&N);
        memset(head,-1,sizeof head);
        FF(i,N-1){
            I("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        FF(i,N){    //遍历每个点,求最长距离 
            dfs(i,0);
        }
        O("%d",(21+maxD)*maxD/2);
        return 0;
    }
  • 相关阅读:
    rogue 源码 curses图形库实现的
    FreeBSD命令(抄至网络)
    没动力没精神
    加班无聊逛论坛
    2009年的FreeBSD命令(转载的)
    freebsd上源码安装cmake
    freeBSD中fetch下载工具使用
    Mac 安装HomeBrew 踩坑总结
    mybatis插入数据时,自动获取主键的自增id
    java 8对List的处理
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8650355.html
Copyright © 2011-2022 走看看