日韩性视频-久久久蜜桃-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(树的直径#入门)的全部內容,希望文章能夠幫你解決所遇到的問題。

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