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

思路:有向图。假设在X牧场参加party,从X回家的时候,以X为起点,使用一次Dijkstra算法即可。难点在于去X参加party的最短路如何求解。
这时候我们可以反向建图,即把原来有向图的方向全部反向,形成一幅新的有向图G',此时再对G'使用一次以X为起点的Dijkstra算法即
可求得原图G中其他各点以X为终点的最短路径。
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring> #define INF 1000000000 using namespace std; int dis1[]; // 正向最短路 (回家的最短路)
int dis2[]; // 反向最短路 (去party的最短路)
int vis[];
int g1[][]; // 正向建图
int g2[][]; // 反向建图
int N, M, X; void dijkstra(int start, int dis[], int g[][])
{
for(int i = ; i <= N; ++i)
{
dis[i] = INF;
vis[i] = ;
} dis[start] = ;
while()
{
int mark = -, minDis = INF;
for(int i = ; i <= N; ++i)
{
if(!vis[i] && dis[i] < minDis)
{
minDis = dis[i];
mark = i;
}
}
if(mark == -)
break;
vis[mark] = ;
for(int i = ; i <= N; ++i)
{
if(!vis[i])
dis[i] = min(dis[i], dis[mark]+g[mark][i]);
} } } int main()
{
scanf("%d %d %d", &N, &M, &X);
for(int i = ; i <= N; ++i)
{
for(int j = ; j <= N; ++j)
{
if(i == j)
g1[i][j] = g2[i][j] = ;
else
g1[i][j] = g2[i][j] = INF;
}
}
for(int i = ; i <= M; ++i)
{
int a, b, cost;
scanf("%d %d %d", &a, &b, &cost);
g1[a][b] = cost;
g2[b][a] = cost;
} dijkstra(X, dis1, g1);
dijkstra(X, dis2, g2); int ans = -; for(int i = ; i <= N; ++i)
{
if(dis1[i] + dis2[i] > ans)
ans = dis1[i] + dis2[i];
} printf("%d\n", ans); return ;
}
Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)的更多相关文章
- ShortestPath:Silver Cow Party(POJ 3268)
牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...
- kuangbin专题专题四 Silver Cow Party POJ - 3268
题目链接:https://vjudge.net/problem/POJ-3268 题意:点X处开办排队,其他点的牛到X点去参加派对,然后从X点回到各自的点,通路是单向的,所有牛都要走最短路, 求出所有 ...
- Silver Cow Party POJ - 3268
#include<iostream> #include<queue> #include<cstring> using namespace std; +,INF=0x ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
随机推荐
- 虚拟机上CentOS7 配置NAT模式
1. 虚拟机网络适配器选择NAT模式 2. 使用vi编辑/etc/sysconfig/network-scripts/ifcfg-ens32 vi /etc/sysconfig/network-scr ...
- LUOGU P4281 [AHOI2008]紧急集合 / 聚会 (lca)
传送门 解题思路 可以通过手玩或打表发现,其实要选的点一定是他们三个两两配对后其中一对的$lca$上,那么就直接算出来所有的$lca$,比较大小就行了. #include<iostream> ...
- arguments的介绍(一)
arguments 是一个类数组对象.代表传给一个function的参数列表. 1.1 arguments length arguments 是个类数组对象,其包含一个 length 属性,可以用 a ...
- 33 N皇后问题
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens/# n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整 ...
- Android基础控件ListView和自定义BaseAdapter适配器
1.简介 ListView用于列表显示,相当于OC中的TableView,和适配器一块使用,相关属性: footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条, ...
- 16_k近邻算法总结
1.k近邻算法属于分类算法 2.你的“邻居”来推断出你的类别 3.标准定义:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别. 4.计算 ...
- 关于排序--sort()和qsort()使用
一.sort()函数的使用 使用sort()函数的时候要加上头文件#include<algorithm>和using namespace std. 这个函数接收两个或者三个参数. 第一个参 ...
- PKU--1267 Cash Machine(多重背包)
题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...
- Activiti历史查看
流程执行完毕后,究竟去了哪里有些疑问. 虽然已完成的任务在act_ru_task和act_ru_execution表中都已被删除,但是这些数据还存在activiti的数据库中,作为历史改由Histor ...
- sscanf linux-c从一个字符串中读进与指定格式相符的数据
https://www.cnblogs.com/lanjianhappy/p/6861728.html 函数原型: Int sscanf( string str, string fmt, mixed ...