Poj 3268 Silver Cow Party (最短路)
題意:一張N*N的有向圖,M條邊,每個點上都有一頭牛。給出一個X,表示牛的目的地。求出所有牛到達X的最短路。然後再求出所有牛回到自己原來位置的最短路。然後計算哪頭牛使用時間最長。
題解:兩遍dijkstra,搞定。詳情看程式碼註釋。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int maxn = 1005; const int INF = 1e9; int map[maxn][maxn]; //地圖 int dis_to[maxn]; //各點到X的最短路 int dis_from[maxn]; //從X到各點的最短路 int vis_to[maxn]; //標記陣列1 int vis_from[maxn]; // 標記陣列2 int N; void dijkstra(int star){ for(int i = 1; i <= N ; i++){ //初始化 dis_to[i] = map[star][i]; dis_from[i] = map[i][star]; vis_from[i] = vis_to[i] = 0; } vis_from[star]=vis_to[star]=1; // 標記X點,可以少計算一些資料。 for(int i = 1;i < N; i ++ ){ // 計算從各點到X的最短路 int minn = INF; int point ; for(int j = 1; j <= N; j++){ if(vis_to[j] == 0 && dis_to[j] < minn){ // 每次取出最小的邊 minn = dis_to[j]; point = j; } } vis_to[point] = 1; for(int j = 1; j <= N ; j++) if(vis_to[j] == 0 && dis_to[j] > dis_to[point]+map[point][j]) // 用最小的邊,來更新到達X的最小是,是否能通過這個點來鬆弛其它點 dis_to[j] = dis_to[point] + map[point][j]; } for(int i = 1; i < N ; i++){ //X到各點的最短路 int minn = INF; int point ; for(int j = 1; j <= N ; j++){ if(vis_from[j] == 0 && dis_from[j] < minn){ // 每次取出最小的邊 minn = dis_from[j]; point = j; } } vis_from[point] = 1; for(int j = 1; j <= N ; j++){ if(vis_from[j] == 0 && dis_from[j] > dis_from[point] + map[j][point]) //用最小的邊,來更新到達j點的最小值 dis_from[j] = dis_from[point] + map[j][point]; } } } int main(){ int M,X; scanf("%d%d%d",&N,&M,&X); for(int i = 1; i <= N; i++) //初始化地圖 for(int j = 1; j <= N ; j++) i == j ? map[i][j] = 0 : map[i][j] = map[j][i] = INF; while(M--){ int u,v,w; scanf("%d%d%d",&u,&v,&w); if(map[u][v] > w) //處理重複邊 map[u][v] = w; } dijkstra(X); int max_num = -1; for(int i = 1; i <= N ; i++) // 求出來回最大值。 if(dis_to[i] + dis_from[i] > max_num) max_num = dis_to[i] + dis_from[i]; printf("%d\n",max_num); return 0; }
相關推薦
Poj 3268 Silver Cow Party (最短路)
題意:一張N*N的有向圖,M條邊,每個點上都有一頭牛。給出一個X,表示牛的目的地。求出所有牛到達X的最短路。然後再求出所有牛回到自己原來位置的最短路。然後計算哪頭牛使用時間最長。 題解:兩遍dijkstra,搞定。詳情看程式碼註釋。 #include<cstdio&g
poj 3268 Silver Cow Party 最短路/dijkstra
#include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> #include <i
POJ--3268 Silver Cow Party(最短路)
iostream {} push prior namespace sca author sil main 題目電波:POJ--3268 Silver Cow Party 跑兩遍 dijkstra 就好了 弱智題 #include<iostream&
poj 3268 Silver Cow Party 【最短路,有向圖】
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18401 Accepted: 8421 Description One cow from each of N
POJ 3268 Silver Cow Party(雙向最短路)
Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow p
POJ 3268 Silver Cow Party (最短路,置換矩陣)
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X
POJ 3268 Silver Cow Party
turn border can rate hat n) output conn stream Language: Default Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K To
POJ-3268-Silver Cow Party(迪傑斯特拉 多點到star和star到多點)
D - Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice P
poj 3268 Silver Cow Party 題解
大意:有n個牧場,編號1到n,每個牧場有一頭奶牛。現在所有奶牛要到編號為x的牧場聚會,路徑是單向的,奶牛都很聰明,只走最短路徑,問哪頭奶牛來回走的路徑之和最大,輸出這個最大值。 思路:建立兩個鄰接表(一個出邊表,一個入邊表),然後分別對兩個鄰接表使用一次SPFA,得到的路
【洛谷】1821 [USACO07FEB]銀牛派對Silver Cow Party 最短路徑
題目傳送門 日常水一波,就是對正向圖跑一遍SPFA,然後建一張反向圖,也就是求其他所有的節點到源點的距離,然後再跑一遍SPFA。 最後輸出兩次距離和的最大值就行了。 附上AC程式碼: #includ
POJ-3268 -D - Silver Cow Party(最短路)
題目描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A tota
【題解:POJ3268 Silver Cow Party】(最短路問題)
傳送門:(http://poj.org/problem?id=3268) 題目描述: 農場有N(1≤N≤1000)個牛棚,每個牛棚都有1只奶牛要參加在X牛棚舉行的奶牛派對.共有M(1≤M≤100000)條單向路連線著牛棚,第i條踣需要Ti的時間來通過.牛們都很懶,所以不管是前去X牛棚參加派對
poj3268 Silver Cow Party【最短路】
題目連結:http://poj.org/problem?id=3268 題意:有n個農場,在x號農場有一個派對,農場之間有m條單向邊連線著這些農場,每條邊有一定的時間花費,每隻牛都會從自己農場出發去參
Dijkstra演算法模板(POJ 3268)很好的理解題 Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29507 Accepted: 13395 Description One cow from ea
Silver Cow Party(最短路 + Dijkstra + 鄰接表 + 優先佇列)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11348 Accepted: 5077 Description One cow from each of N farms (1 ≤ N
Silver Cow Party(最短路 很有意思的題)
Line 1: One integer: the maximum of time any one cow must walk. 求兩次最短路,第一次求x到其餘各點的最短路,第二次求各點到x的最短路。前者易於解決,直接應用spfa或其他最短路演算法即可,後者要先將鄰接矩陣轉置再執行最短路演算法。 為什麼進
Silver Cow Party 【雙向最短路】
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going
Silver Cow Party POJ
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X
Silver Cow Party (定點X為起點和終點的最長距離)
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X
POJ - 1062 昂貴的聘禮(最短路Dijkstra)
方案 ref pst 思路 記錄 ron inpu 一個點 != 昂貴的聘禮 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Subm