日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj1985 Cow Marathon(树的直径#入门)

發布時間:2024/9/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj1985 Cow Marathon(树的直径#入门) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

poj1985 Cow Marathon(樹的直徑)


Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 9110 Accepted: 4171 Case Time Limit: 1000MS 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 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

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.
Source

USACO 2004 February

思路

先搞清樣例輸入:
from,to,val,direction(from到to有一條長val的邊,to在from的direction方向)
這里不用考慮方向,直接建雙向邊。
題意:求解兩個牧場之間最長的距離。
即求樹的直徑即可。
樹的直徑求解步驟:

  • 任意找一點作為起始點,找到離他最遠的一點pos1,根據樹直徑的性質,此時pos1一定是樹直徑的一個端點。
  • 把pos1當做起點,找離pos1最遠的點pos2;
    pos1------pos2即為樹的直徑。
  • 方法:

  • 兩遍dfs或者兩遍bfs。
  • 樹形dp
  • AC代碼:

    • 法1:兩遍dfs(bfs略):
    • 時間復雜度O(n)
    • 優點:好記錄路徑
    • 缺點:寫起來相對復雜一點點
    #include <iostream> #include <cstring> #include <string> using namespace std; const int N = 1e5+5; struct Edge {int from;int to;int val;int nxt; }edge[N]; int head[N<<1],idx; void init() {memset(head,-1,sizeof(head));idx = 0; } inline void add_edge(int from,int to,int val) {edge[idx].from = from;edge[idx].to = to;edge[idx].val = val;edge[idx].nxt = head[from];head[from] = idx++; } bool vis[N]; int pos1,pos2,maxx; void dfs(int s,int dis) {if(dis > maxx){pos1 = s;maxx = dis;//cout<<pos1<<endl;}for(int i = head[s]; ~i;i = edge[i].nxt){int to = edge[i].to;int val = edge[i].val;if(!vis[to]){vis[to] = true;dfs(to,dis+val);}}return; } int main() {int n,m;cin>>n>>m;int x,y,v;string d;init();while(m--){cin>>x>>y>>v>>d;add_edge(x,y,v);add_edge(y,x,v);}memset(vis,false,sizeof(vis));maxx = 0;dfs(1,0);//cout<<pos1<<endl;pos2 = pos1;maxx = 0;memset(vis,false,sizeof(vis));dfs(pos1,0);//cout<<pos1<<endl;cout<<maxx<<endl;return 0; }

    簡化代碼:

    #include <iostream> #include <cstring> #include <string> using namespace std; const int N = 1e5+5; struct Edge {int from;int to;int val;int nxt; } edge[N]; int head[N<<1],idx; void init() {memset(head,-1,sizeof(head));idx = 0; } inline void add_edge(int from,int to,int val) {edge[idx].from = from;edge[idx].to = to;edge[idx].val = val;edge[idx].nxt = head[from];head[from] = idx++; } int pos1,pos2,maxx; void dfs(int u,int fa,int dis) {if(dis > maxx){pos1 = u;maxx = dis;}for(int i = head[u]; ~i; i = edge[i].nxt){int to = edge[i].to;int val = edge[i].val;if(to != fa){dfs(to,u,dis+val);}} } int main() {int n,m;cin>>n>>m;int x,y,v;string d;init();while(m--){cin>>x>>y>>v>>d;add_edge(x,y,v);add_edge(y,x,v);}dfs(1,0,0);pos2 = pos1;dfs(pos1,0,0);cout<<maxx<<endl;return 0; }
    • 法2:dp
    • 時間復雜度O(n)
    • 優點:寫起來簡單
    • 缺點:不好記錄路徑
    #include <iostream> #include <cstring> #include <string> using namespace std; const int N = 1e5+5; struct Edge {int from;int to;int val;int nxt; } edge[N]; int head[N<<1],idx; void init() {memset(head,-1,sizeof(head));idx = 0; } inline void add_edge(int from,int to,int val) {edge[idx].from = from;edge[idx].to = to;edge[idx].val = val;edge[idx].nxt = head[from];head[from] = idx++; } int maxx; int dp[N]; void DP(int u,int fa) {for(int i = head[u]; ~i; i = edge[i].nxt){int to = edge[i].to;int val = edge[i].val;if(to != fa){DP(to,u);maxx = max(maxx,dp[u]+dp[to]+val);dp[u] = max(dp[u],dp[to]+val);}}return; } int main() {int n,m;cin>>n>>m;int x,y,v;string d;init();while(m--){cin>>x>>y>>v>>d;add_edge(x,y,v);add_edge(y,x,v);}DP(1,0);cout<<maxx<<endl;return 0; }

    總結

    以上是生活随笔為你收集整理的poj1985 Cow Marathon(树的直径#入门)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 精品一区在线播放 | 免费一级特黄 | 灌篮高手全国大赛电影 | 337p嫩模大胆色肉噜噜噜 | 人妻少妇精品中文字幕av蜜桃 | www.狠狠艹 | 成人福利av| 成人福利小视频 | 久热最新 | 久久手机视频 | 91视频网页| 国产精品成人一区二区三区 | 欧美高清一区二区三区四区 | 成人免费xxxxxx视频 | 无码精品一区二区三区AV | 久久国产乱 | 国产三区视频 | 波多野结衣在线网址 | 日日干天天射 | 久久久久噜噜噜亚洲熟女综合 | 亚洲精品一区二区三区四区五区 | 91精品久久久久久久久 | japanese中文字幕 | 村姑电影在线播放免费观看 | 春闺艳妇(h)高h产乳 | 久久精品欧美一区 | 日本一二三区不卡 | 四虎永久网址 | 欧美男女激情 | 可以直接观看的av | 久久久久久久国产精品毛片 | 四虎国产精品永久免费观看视频 | 青青久在线视频 | 色干综合 | 深夜视频一区二区三区 | 亚洲第十页 | 男人都懂的网址 | 色午夜av | 欧美日韩在线免费播放 | 污视频网站入口 | 国产高清一区在线观看 | 亚洲一区欧美一区 | 毛片免费全部无码播放 | 欧美精品不卡 | 日一区二区三区 | 韩日一级片 | 国产日批视频在线观看 | 91av短视频| 首尔之春在线看 | 亚洲www.| 欧美另类视频在线观看 | 手机在线一区二区三区 | 久久久综合视频 | www.av在线| 黄色wwwww | 一区二区www | 日韩欧美的一区二区 | 先锋影音亚洲 | 欧美精品一区在线发布 | 男人的天堂网av | 色婷婷av一区二区三区gif | 天堂影院一区二区 | 91学生片黄 | 欧美天堂在线观看 | 大片av | 亚洲人成色777777老人头 | 人妻丰满熟妇无码区免费 | 三级欧美日韩 | 亚洲国产精品欧美久久 | 亚州av影院 | 国产xxxxx在线观看 | www.青青操| 亚洲一级一级 | 韩国av一区二区 | 法国空姐在线观看视频 | 国产aaaaaaa| 国产精品欧美综合 | 国产99免费 | 欧美激情18 | 夫妻啪啪呻吟x一88av | 熟女高潮一区二区三区 | 国产91精品久久久久久久 | 成年人免费在线观看网站 | 国产伦子伦对白视频 | 欧美精品一级在线观看 | 精品成人无码久久久久久 | 亚洲AV乱码国产精品观看麻豆 | 5566色 | 欧美一级久久久 | 欧美特级一级片 | 91se在线| 一区二区三区伦理 | 欧美图片自拍偷拍 | 污污视频免费观看 | 国产免费一区二区视频 | 亚欧成人 | 不卡的在线视频 | 亚洲码无人客一区二区三区 | 黑人巨茎大战欧美白妇 |