B - Cow Marathon DFS+vector存图

After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms. 
有n个农田和m条路,以及每条路的方向(方向在这道题中没有用),求最长的一条路,也就是两点间的最大距离,即树的直径.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Each line contains four space-separated entities, F1,
        F2, L, and D that describe a road. F1 and F2 are numbers of
        two farms connected by a road, L is its length, and D is a
        character that is either ‘N‘, ‘E‘, ‘S‘, or ‘W‘ giving the
        direction of the road from F1 to F2.

Output

* Line 1: An integer giving the distance between the farthest pair of farms.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S

Sample Output

52

题目大意:从点A到点B的距离是C,有N个点,M条线,问两点之间最远的距离;思路:首先按要存图,一开开始是用的数组,一直RE后来才发现数的范围是1E5,二维数组直接就蹦了,所以要用Vector存图,输入的时候也要用c语言的常规输入输出,不然会TLE

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int n,m;//m个农场,6条路径
struct stu{
    int a,b;//保存点2和点1 到点2点距离
}e;

vector<stu >arr[100100];
int mark[100010];//标记数组
int ans=0;
int xx;

void dfs(int x,int step){
    if(step>ans){
        ans=step;
        xx=x;
    }
    for(int i=0;i<arr[x].size();i++){//arr[x]中的点肯定是与x相连接的点
        if(mark[arr[x][i].a]==0){
            mark[arr[x][i].a]=1;
            dfs(arr[x][i].a,step+arr[x][i].b);
            mark[arr[x][i].a]=0;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    int x,y,z;
    char s;
    memset(arr,0,sizeof(arr));
    for(int i=0;i<m;i++){
//        scanf("%d%d%d %c\n",&x,&y,&z);
//        cin>>x>>y>>z>>s;
        scanf("%d %d %d",&x,&y,&z);
        getchar(), getchar();
//        getchar();
//        getchar();getchar();
        arr[x].push_back({y,z});
        arr[y].push_back({x,z});
//        arr[x][y]=z;
//        arr[y][x]=z;

    }

    ans=0;
    memset(mark,0,sizeof(mark));
    mark[1]=1;
    dfs(1,0);
    memset(mark,0,sizeof(mark));
    mark[xx]=1;
    dfs(xx,0);//两轮dfs直接输出
    cout<<ans<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/Accepting/p/11243635.html

时间: 2024-08-01 01:12:40

B - Cow Marathon DFS+vector存图的相关文章

POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17497   Accepted: 7398 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

Neither shaken nor stirred(DFS理解+vector存图)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2013 题目理解: 给定n个点的有向图: 下面n行,第一个数字表示点权,后面一个数字m表示有m条边. 起点是1. 对于每个点,输出2个值表示前驱点权1和该点点权2. 1.就是若有多条路径且全为“同一个值”输出“同一个值”,否则输出unknown: 若有一条路径若前驱节点的权值>0,输出前驱结点的权值,否则输出sober; 2.否则若该点点权>0输出该点点权,否则输出前驱点权值(若有多条

从存图到最短路算法的图论总结

INTRODUCTION: 图论算法在计算机科学中扮演着很重要的角色,它提供了对很多问题都有效的一种简单而系统的建模方式.很多问题都可以转化为图论问题,然后用图论的基本算法加以解决.--百度百科 对于OI而言,图是指由若干给定的点及若干条连接两点的线(边)所构成的图形 借助图论知识,我们往往可以将一些复杂的问题转化到基础的图论算法上,进而使用已有算法解决全新问题 那么想如果想要运用图论,首先要从存图开始 前排感谢教我图论的周润喵老师,syc学长,就序老师 可是我还是没学会 一,存图 对于一个图而

Safe Path(bfs+一维数组存图)

题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到  2<=n*m<=200000 时,就不可以用二维数组存图了,不过据说因为数据比较水,可以用vector存图: vector存图AC代码: 1 /* */ 2 # include <iostream> 3 # include <stdio.h> 4 # include <st

poj1985 Cow Marathon --- 树的直径

树的直径即树中最长的路径的长度. 用两次dfs,第一次从任意点出发求得一个最远点p, 第二次从p出发求得最远点,这条路径就是最长路,即所求. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include &l

Cow Marathon

poj1985:http://poj.org/problem?id=1985 题意:就是树的直径. 题解:直接DFS即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 const int N=1e5+10; 8 struct Node{ 9

最短路 spfa 算法 &amp;&amp; 链式前向星存图

推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/details/54379641 spfa  自行百度 说的很详细 spfa 有很多实现的方法  dfs  队列  栈  都可以 时间复杂度也不稳定 不过一般情况下要比bellman快得多 #include <stdio.h> #include <math.h> #include <st

Count on a tree SPOJ 主席树+LCA(树链剖分实现)(两种存图方式)

Count on a tree SPOJ 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)[GO!] 题意 是说有棵树,每个节点上都有一个值,然后让你求从一个节点到另一个节点的最短路上第k小的值是多少. 解题思路 看到这个题一想以为是树链剖分+主席树,后来写着写着发现不对,因为树链剖分我们分成了一小段一小段,这些小段不能合并起来求第k小,所以这个想法不对.奈何不会做,查了查题解,需要用LCA(最近公共祖先),然后根据主席树具有区间加减的性质,我们

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include