(最短路)Silver Cow Party --POJ--3268
题目链接:
http://poj.org/problem?id=3268
题意:
先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA)
迪杰斯特拉:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std; #define N 1005
#define INF 0xfffffff int n, m, x;
int a, b, t;
int G[N][N], dist1[N], dist2[N];
bool vis1[N], vis2[N]; void IN()
{
memset(vis1, false, sizeof(vis1));
memset(vis2, false, sizeof(vis2));
for(int i=; i<=n; i++)
{
dist1[i]=INF;
dist2[i]=INF;
for(int j=; j<=i; j++)
G[i][j]=G[j][i]=INF;
}
} void DIJ1()
{
dist1[x]=;
for(int i=; i<=n; i++)
{
int index=, MIN=INF;
for(int j=; j<=n; j++)
{
if(!vis1[j] && dist1[j]<MIN)
{
index=j, MIN=dist1[j];
}
}
vis1[index]=;
for(int j=; j<=n; j++)
{
dist1[j]=min(dist1[j], dist1[index]+G[index][j]);
}
}
} void DIJ2()
{
dist2[x]=;
for(int i=; i<=n; i++)
{
int index=, MIN=INF;
for(int j=; j<=n; j++)
{
if(!vis2[j] && dist2[j]<MIN)
{
index=j, MIN=dist2[j];
}
}
vis2[index]=;
for(int j=; j<=n; j++)
{
dist2[j]=min(dist2[j], dist2[index]+G[j][index]);
}
}
} int main()
{
while(scanf("%d%d%d", &n, &m, &x)!=EOF)
{
IN(); for(int i=; i<m; i++)
{
scanf("%d%d%d", &a, &b, &t);
G[a][b]=t;
} DIJ1();
DIJ2(); int ans=;
for(int i=; i<=n; i++)
ans=max(ans, dist1[i]+dist2[i]); printf("%d\n", ans);
}
return ;
}
(最短路)Silver Cow Party --POJ--3268的更多相关文章
- Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)
思路:有向图.假设在X牧场参加party,从X回家的时候,以X为起点,使用一次Dijkstra算法即可.难点在于去X参加party的最短路如何求解. 这时候我们可以反向建图,即把原来有向图的方向全部反 ...
- kuangbin专题专题四 Silver Cow Party POJ - 3268
题目链接:https://vjudge.net/problem/POJ-3268 题意:点X处开办排队,其他点的牛到X点去参加派对,然后从X点回到各自的点,通路是单向的,所有牛都要走最短路, 求出所有 ...
- ShortestPath:Silver Cow Party(POJ 3268)
牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...
- 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——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 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(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- 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 ...
随机推荐
- 9.13 h5日记
9.13 面试题 为什么两个P出此案的效果不同,原因是 浏览器在解析第二个P的时候,因为字母没有空格,浏览器会认为这个单词没有打完,所以不会换行. 列表 ul ol dl li 1.无序列表 ul ( ...
- 对于读txt文件一点总结
txt 内容 中间有比如如空格,制表符(tab)在txt为空格符(Spaces).回车符.换行符,有空字符串等情况,在读取过滤中要充分考虑到 1:打开文件 var sr=new StreamReade ...
- js sort
排序算法 比较的过程必须通过函数抽象出来.通常规定,对于两个元素x和y,如果认为x < y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法就不用关 ...
- 5D - Rectangles
Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have t ...
- hdu 5459(2015沈阳网赛) Jesus Is Here
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意 给出一组字符串,每个字符串都是前两个字符串相加而成,求第n个字符串的c的各个坐标的差的和,结果 ...
- tmux 快捷操作
-- 基本使用 tmux # 运行 tmux -2 以256终端运行 C-b d # 返回主 shell , tmux 依旧在后台运行,里面的命令也保持运行状态 tmux ls # 显示已有tm ...
- win8+iis8+PHP5安装配置和Zend Optimizer安装教程
安装 Zend Optimizer 下载地址:http://www.onlinedown.net/soft/32228.htm 下载直接双击安装即可,安装过程要你选择 Web Server ...
- [Python]Python章1 Python中_的故事
_xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. 本文为译文,版 ...
- java集合示例 小心重载的陷阱
package com.hra.riskprice; import com.hra.riskprice.SysEnum.Factor_Type; import org.springframework. ...
- linux-ubuntu 下R无法安装HH包的原因及解决方案
错误信息: configure: error: GNU MP not found, or not 4.1.4 or up, see http://gmplib.org ERROR: configura ...