POJ - 3268 Silver Cow Party(dijkstra技巧)

题目链接:http://poj.org/problem?id=3268

题意:n只奶牛(分别在1-n奶牛舍)分别从各自的奶牛舍出发到X奶牛舍,然后回到自己的奶舍(都以最短路),求出哪一只奶牛花费的距离最远。

题解:n到达1000,想直接Floyd肯定不行。

从X奶牛舍回家,就直接以X为源点最短路就可以了;从各自的奶牛舍去X奶牛舍可以以X为源点反方向进行求最短路。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7
 8 const int N=1111;
 9 const int INF=0x3f3f3f3f;
10 int d1[N],d2[N];
11 vector < pair<int,int> > E1[N];
12 vector < pair<int,int> > E2[N];
13
14 void init(){
15     for(int i=0;i<N;i++) d1[i]=INF,d2[i]=INF;
16 }
17
18 void dijkstra(int d[],int st,vector < pair<int,int> > E[]){
19     priority_queue < pair<int,int> > Q;
20     d[st]=0;
21     Q.push(make_pair(-d[st],st));
22     while(!Q.empty()){
23         int now=Q.top().second;
24         Q.pop();
25         for(int i=0;i<E[now].size();i++){
26             int v=E[now][i].first;
27             int D=E[now][i].second;
28             if(d[v]>D+d[now]){
29                 d[v]=D+d[now];
30                 Q.push(make_pair(-d[v],v));
31             }
32         }
33     }
34 }
35
36 int main(){
37     int n,m,x;
38     scanf("%d %d %d",&n,&m,&x);
39     int a,b,c;
40     for(int i=0;i<m;i++){
41         scanf("%d %d %d",&a,&b,&c);
42         E1[a].push_back(make_pair(b,c));
43         E2[b].push_back(make_pair(a,c));
44     }
45     init();
46     int ans=-1;
47     dijkstra(d1,x,E1);
48     dijkstra(d2,x,E2);
49     for(int i=1;i<=n;i++) ans=max(ans,d1[i]+d2[i]);
50     printf("%d",ans);
51     return 0;
52 }
时间: 2024-10-11 21:23:01

POJ - 3268 Silver Cow Party(dijkstra技巧)的相关文章

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 Description 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 total of \(M (1 ≤ M ≤ 100,000)\) unidirectional (one-way roads

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 Description 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 最短路—dijkstra算法的优化。

POJ 3268 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 party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 3268 Silver Cow Party(SPFA)

Description 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 total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description 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(最短路)

poj 3268 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 party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 Description 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