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 ...
随机推荐
- React Native & Android & iOS & APK
React Native & Android & iOS & APK https://play.google.com/apps/publish/signup/ $ 25 bui ...
- 一道php笔试题
原文地址: http://www.walu.cc/php/a-bishiti.md 问题: 请找出下面代码中的问题,修复并优化. <?php //批量注册用户,每次>100个. //注册新 ...
- js 调用打印机方法
<button onclick="localdy({php echo $item['order']['id'];})" class="btn btn-xs orde ...
- java返回json设置自定义的格式
使用注解@JsonSerialize(using = CustomPriceSerialize.class) 创建自定义的格式化类(可为内部类) /** * 设置默认返回的小数类型(0.01 元) * ...
- Spring Boot学习总结二
Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Redis还提供一些类 ...
- MAC上有哪些优秀的日常软件| 入门级Mac OS 用户必备软件
本文整理的网友反馈的MAC上有哪些优秀的日常软件+入门级Mac OS 用户必备软件,感兴趣的朋友可以看看,下载下来试用一样便知实不实用.如有更好的推荐,欢迎留言. MAC上有哪些优秀的日常软件 Tim ...
- Linux性能优化实战:系统的swap变高(09)
一.实验环境 1.操作系统 root@openstack:~# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu ...
- 源码-mybatis-01-SqlSessionFactory创建
0.总概 1.SqlSessionFactory在mybatis只要创建一次: import com.suntek.vdm.gw.util.AESEncipher; import org.apac ...
- dataTable 实战总结
后台项目中经常会用到 dataTable 进行表格的智能绘制:总结一下项目中经常用到的 dataTable 设置 1.dataTable 属性设置: autoWidth: true, // 是否自动计 ...
- 堆排序(heap sort)
参考博客:http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/ 1.二叉树 二叉树的第 i 层至多有 2i-1 个结点:深度为 k 的二叉树至多 ...