bzoj 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 树的直径
来源:互联网 发布:软件就业班 编辑:程序博客网 时间:2024/05/18 17:39
→题目链接←
【想说的话】
一开始以为还要像前面一题那样搞带权并查集
然后我就不会了...
后来突然发现我zz了
最后一个方向没用了...直接建双向边然后跑树的直径就行了...
幸亏这个题保证边不交叉,要不然就彻底不会了
【题解】
建双向边然后跑树的直径
找树的直径:随便从一个点跑bfs,找到距离它最远的那个点,再从那个点跑bfs,最长路就是树的直径
【代码】
#include<iostream>#include<cstdio>#include<algorithm>#include<set>#include<vector>#include<queue>#define inf 233333333using namespace std;struct node{int to,len;node(int x,int y){to=x,len=y;}};int n,m;vector<node>v[40020];bool vis[40020];int dis[40020];void spfa(int s){for(int i=1; i<=n; i++)dis[i]=inf,vis[i]=0;queue<int>q;q.push(s);dis[s]=0;vis[s]=true;while(!q.empty()){int t=q.front();q.pop();for(int i=0; i<v[t].size(); i++){int to=v[t][i].to;int len=v[t][i].len;if(dis[to]>dis[t]+len){dis[to]=dis[t]+len;if(!vis[to])q.push(to),vis[to]=true;}}vis[t]=false;}}int main(){scanf("%d%d",&n,&m);for(int i=0; i<m; i++){int x,y,z;char c[2];scanf("%d%d%d%s",&x,&y,&z,c);v[x].push_back(node(y,z));v[y].push_back(node(x,z));}spfa(1);int Max=0,x;for(int i=2; i<=n; i++)if(Max<dis[i])Max=dis[i],x=i;spfa(x);Max=0;for(int i=1; i<=n; i++)Max=max(Max,dis[i]);printf("%d\n",Max);return 0;}
阅读全文
0 0
- 【BZOJ 3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松【树的直径】
- bzoj 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 树的直径
- POJ - 1985 Cow Marathon 奶牛马拉松 计算树的直径
- BZOJ 3363 POJ 1985 Cow Marathon 树的直径
- Cow Marathon 【树的直径】
- bzoj 3366: [Usaco2004 Feb]Breeding 奶牛饲育
- POJ 1985 Cow Marathon 树的直径
- POJ 1985 Cow Marathon 树的直径
- poj1985 Cow Marathon --- 树的直径
- 【树的直径】 POJ 1985 Cow Marathon
- poj 1985 Cow Marathon 【树的直径】
- POJ 1985 Cow Marathon(树的直径)
- poj 1985 Cow Marathon 【树的直径】
- POJ1985 Cow Marathon 树的直径
- poj-1985-Cow Marathon【树的直径】
- poj 1985 Cow Marathon 树的直径
- Poj 1985 Cow Marathon ( 树的直径
- [学习][poj1985]树的直径 Cow Marathon
- 公交卡植入手机
- Java中解决 cannot be cast to java.io.Serializable的问题
- 如何使一个程序运行唯一实例
- bzoj 4515: [Sdoi2016]游戏
- css的样式选择优先级
- bzoj 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 树的直径
- bzoj1207: [HNOI2004]打鼹鼠(Dp)
- localStorage存储
- ECS、域名、IP
- numpy中数组广播
- eclipse集成反编译工具jd-gui
- boost延时函数sleep
- 页面传值不交互:JS存储数据之LocalStorage用法
- JsonObject中的optDouble方法