<poj1985>Cow Marathon
来源:互联网 发布:身份证核查软件 编辑:程序博客网 时间:2024/04/28 10:34
Description
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.
Input
* Lines 1.....: Same input format as "Navigation Nightmare".
Output
* Line 1: An integer giving the distance between the farthest pair of farms.
Sample Input
7 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S
Sample Output
52
Hint
The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52.
没什么难度 直接套模板
#include<cstdio>#include<queue>#include<cstring>using namespace std;struct node {int from,to,val,next;};node dian[1000001];int cut;int head[100001];void chushihua(){cut=0;memset(head,-1,sizeof(head));}void Edge(int u,int v,int w){dian[cut].from =u;dian[cut].to =v;dian[cut].val =w;dian[cut].next =head[u];head[u]=cut++;}int jilu;int vis[100001];int dist[100001];int ans;int n,m;void bfs(int s){memset(vis,0,sizeof(vis));memset(dist,0,sizeof(dist));ans=0;queue<int>q;q.push(s);vis[s]=1;dist[s]=0;while(!q.empty() ){int u=q.front() ;q.pop() ;for(int i=head[u];i!=-1;i=dian[i].next ){int v=dian[i].to ;if(!vis[v]){if(dist[v]<dist[u]+dian[i].val )dist[v]=dist[u]+dian[i].val ;vis[v]=1; q.push(v); }} }for(int i=1;i<=n;i++){if(ans<dist[i]) { ans=dist[i]; jilu=i; }}}int main(){scanf("%d%d",&n,&m); chushihua();int a,b,c,d;for(int i=1;i<=m;i++){scanf("%d%d%d%c",&a,&b,&c,&d);getchar();Edge(a,b,c);Edge(b,a,c);}bfs(1);bfs(jilu);printf("%d\n",ans);return 0;}
0 0
- poj1985 - Cow Marathon
- POJ1985 Cow Marathon
- POJ1985 Cow Marathon
- <poj1985>Cow Marathon
- [POJ1985]Cow Marathon
- POJ1985 Cow Marathon
- POJ1985——Cow Marathon
- poj1985 Cow Marathon --- 树的直径
- POJ1985 Cow Marathon 树的直径
- [学习][poj1985]树的直径 Cow Marathon
- POJ1985 Cow Marathon——树的最长链
- POJ1985 Cow Marathon (DP/BFS 树的直径) #by Plato
- poj1985 Cow Marathon(求树的直径)
- 【poj1985】Cow Marathon 树形DP求树的直径
- 【POJ1985】Cow Marathon(基础 树的直径)
- Cow Marathon
- Cow Marathon
- Cow Marathon
- OkHttp使用完全教程
- 【日常收获5】相机效果之上绘图
- Red and Black
- 1_lua的第一个程序--hello world
- 杭电-2196 Computer(每个点的最大路径)
- <poj1985>Cow Marathon
- sqlite
- android 获取屏幕分辨率
- 数据库连接池
- hdu 2068 RPG的错排 (错排公式)
- DS18B20温度传感器 - arduino
- java 容器
- POJ 1125 Stockbroker Grapevine floyd多源最短路,选取一个点,使最大边的权值是最小的
- E - Farthest Nodes in a Tree