poj 3268 Silver Cow Party(最短路dijkstra)
描述:
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 requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
题解:
要求的其实就是1-n的每个点到x以及从x回来的最短路之和的最大值。就可以用两次dijkstra算法求解,一次算从i到x,一次算从x到i,最后求得相加的最大值即可。
代码:
#include <iostream>
#include <stdio.h> using namespace std;
#define inf 1<<29
int n,m,x;
bool vis[];
int map[][];
int go[],dback[]; //go是从i—>x back是从x—>i int dijkstra()
{
int i,j,f,v;
for(i=;i<=n;i++)
{
vis[i]=;
go[i]=map[i][x];
dback[i]=map[x][i];
} for(i=;i<=n;i++)
{
f=inf;
for(j=;j<=n;j++)
{
if(!vis[j]&&dback[j]<f)
{
v=j;
f=dback[j];
}
}
vis[v]=;
for(j=;j<=n;j++)
if(!vis[j]&&map[v][j]+dback[v]<dback[j])
dback[j]=map[v][j]+dback[v];
} for(i=;i<=n;i++) vis[i]=; for(i=;i<=n;i++)
{
f=inf;
for(j=;j<=n;j++)
{
if(!vis[j]&&go[j]<f)
{
v=j;
f=go[j];
}
}
vis[v]=;
for(j=;j<=n;j++)
{
if(!vis[j]&&map[j][v]+go[v]<go[j])
go[j]=map[j][v]+go[v];
}
} f=-;
for(i=;i<=n;i++)
{
if(go[i]+dback[i]>f)
f=go[i]+dback[i];
}
return f;
} int main()
{
int a,b,c;
while(~scanf("%d%d%d",&n,&m,&x)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i!=j) map[i][j]=inf;
else map[i][j]=;
}
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]=c;
}
printf("%d\n",dijkstra());
}
return ;
}
poj 3268 Silver Cow Party(最短路dijkstra)的更多相关文章
- 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 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 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)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- 【调试工具】tcpdump
[tcpdump]https://linux.cn/article-10191-1.html
- 回忆曾经的SSM框架实现文件上传
近期在使用springboot实现文件上传的功能,想到曾经用SSM做过这个功能,在这里记录一下过去实现的方式 maven添加文件上传所需的依赖 springMVC的配置文件配置一下文件上传 我实现的是 ...
- [ffmpeg] h264并行解码
ffmpeg中的并行解码分为两种: Frame-level Parallelism Slice-level Parallelism Frame-level Parallelism 帧间依赖 我们之前讨 ...
- subprocess
在没有subprocess这个模块的时候,我们怎么去跟我们的操作系统做交互的呐?下面我们先说说这三个模块:os.system().os.popen().commands. 1. os.system( ...
- 数论ex
数论ex 数学学得太差了补补知识点or复习 Miller-Rabin 和 Pollard Rho Miller-Rabin 前置知识: 费马小定理 \[ a^{p-1}\equiv 1\pmod p, ...
- 小白月赛13 小A的路径 (矩阵快速幂求距离为k的路径数)
链接:https://ac.nowcoder.com/acm/contest/549/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- logstash的output配置中指定elasticsearch的template
转自:https://blog.csdn.net/felix_yujing/article/details/78930389 之前采用的是通过filebeat收集nginx的日志,直接到elastic ...
- django 4.get接口开发
根据上一篇文章,有post,那么就有get请求,其余部分不变,就是把post换成get就可以. #views.py from django.http.response import HttpRespo ...
- docker容器网络
1.我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: · host模式,使用--net=host指定 · containe ...
- java 判断语句和循环语句
一.判断语句:if和switch if(关系表达式1) { 语句体1; }else if (关系表达式2) { 语句体2; }…else { 语句体n+; } switch(表达式) { case 值 ...