Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)

思路:有向图。假设在X牧场参加party,从X回家的时候,以X为起点,使用一次Dijkstra算法即可。难点在于去X参加party的最短路如何求解。

   这时候我们可以反向建图,即把原来有向图的方向全部反向,形成一幅新的有向图G‘,此时再对G‘使用一次以X为起点的Dijkstra算法即

      可求得原图G中其他各点以X为终点的最短路径。

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<cmath>
 5 #include<set>
 6 #include<algorithm>
 7 #include<cstdio>
 8 #include<map>
 9 #include<cstring>
10
11 #define INF 1000000000
12
13 using namespace std;
14
15 int dis1[1010];        // 正向最短路 (回家的最短路)
16 int dis2[1010];        // 反向最短路 (去party的最短路)
17 int vis[1010];
18 int g1[1010][1010];    // 正向建图
19 int g2[1010][1010];    // 反向建图
20 int N, M, X;
21
22 void dijkstra(int start, int dis[], int g[1010][1010])
23 {
24     for(int i = 1; i <= N; ++i)
25     {
26         dis[i] = INF;
27         vis[i] = 0;
28     }
29
30     dis[start] = 0;
31     while(1)
32     {
33         int mark = -1, minDis = INF;
34         for(int i = 1; i <= N; ++i)
35         {
36             if(!vis[i] && dis[i] < minDis)
37             {
38                 minDis = dis[i];
39                 mark = i;
40             }
41         }
42         if(mark == -1)
43             break;
44         vis[mark] = 1;
45         for(int i = 1; i <= N; ++i)
46         {
47             if(!vis[i])
48                 dis[i] = min(dis[i], dis[mark]+g[mark][i]);
49         }
50
51     }
52
53
54
55 }
56
57 int main()
58 {
59     scanf("%d %d %d", &N, &M, &X);
60     for(int i = 1; i <= N; ++i)
61     {
62         for(int j = 1; j <= N; ++j)
63         {
64             if(i == j)
65                 g1[i][j] = g2[i][j] = 0;
66             else
67                 g1[i][j] = g2[i][j] = INF;
68         }
69     }
70     for(int i = 1; i <= M; ++i)
71     {
72         int a, b, cost;
73         scanf("%d %d %d", &a, &b, &cost);
74         g1[a][b] = cost;
75         g2[b][a] = cost;
76     }
77
78     dijkstra(X, dis1, g1);
79     dijkstra(X, dis2, g2);
80
81
82     int ans = -1;
83
84     for(int i = 1; i <= N; ++i)
85     {
86         if(dis1[i] + dis2[i] > ans)
87             ans = dis1[i] + dis2[i];
88     }
89
90     printf("%d\n", ans);
91
92     return 0;
93 }

原文地址:https://www.cnblogs.com/FengZeng666/p/11405351.html

时间: 2024-10-08 18:26:50

Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)的相关文章

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 迪杰斯特拉图论 置换找最短路

题目:https://vjudge.net/problem/POJ-3268 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 connect

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(最短路)

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)

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 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 , 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 【最短路Dijkstra + 结构体妙用】

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 (