Silver Cow Party---poj3268(最短路,迪杰斯特拉)
Silver Cow Party
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 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 Titime 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
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define INF 0xfffffff
#define N 1100
using namespace std;
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
int x,n,m,dist1[N],dist2[N],vis[N];
int maps[N][N]; void Dij1()
{
int i,j; vis[x]=; dist1[x]=; for(i=; i <= n; i++)
{
int Min=INF,index=-; for(j = ; j <= n; j++)
{
if(vis[j] == && Min > dist1[j])
{
Min = dist1[j];
index = j;
}
}
if(index==-)break;
vis[index]=; for(j = ; j <= n; j++)
dist1[j] = min(dist1[j],dist1[index] + maps[index][j]);
}
}
void Dij2()
{
int i,j; vis[x]=; dist2[x]=; for(i=; i <= n; i++)
{
int Min=INF,index=-; for(j = ; j <= n; j++)
{
if(vis[j] == && Min > dist2[j])
{
Min = dist2[j];
index = j;
}
}
if(index==-)break;
vis[index]=; for(j = ; j <= n; j++)
dist2[j] = min(dist2[j],dist2[index] + maps[j][index]);
}
} int main()
{
int i,a,b,c;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
for(i=;i<=n;i++)
{
dist1[i]=dist2[i]=INF;
for(int j=;j<=n;j++)
maps[i][j]=INF;
}
while(m--)
{
scanf("%d%d%d", &a, &b, &c); maps[a][b] = c;
} memset(vis,,sizeof(vis));
Dij1();//去 memset(vis,,sizeof(vis));
Dij2();//回来; int ans=; for(i=;i<=n;i++)
{
ans=max(ans,dist1[i]+dist2[i]);
}
printf("%d\n",ans);
}
return ;
}
Silver Cow Party---poj3268(最短路,迪杰斯特拉)的更多相关文章
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 最短路——迪杰斯特拉算法 HDU_3790
初识最短路,今天只弄了一个迪杰斯特拉算法,而且还没弄成熟,只会最基本的O(n^2),想弄个优先队列都发现尼玛被坑爆了,那个不应该用迪杰斯特拉算法写 表示还是不会优化版的迪杰斯特拉算法,(使用优先队列) ...
- POJ 1062 昂贵的聘礼 (最短路 迪杰斯特拉 )
题目链接 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请 ...
- HUD 2544 最短路 迪杰斯特拉算法
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵
Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...
- (最短路)Silver Cow Party --POJ--3268
题目链接: http://poj.org/problem?id=3268 题意: 先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA) 迪杰斯特 ...
- pat1003 迪杰斯特拉法和dfs求最短路
本题的背景是求定点和定点之间的最短路问题(所有的最短路 不是一个解 是全部解,方法手段来自数据结构课程中的迪杰斯特拉算法和dfs(深度优先遍历). 分别用两种方法编程如下代码 dfs #includ ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
随机推荐
- Explaining Delegates in C# - Part 7 (Asynchronous Callback - Way 4)
This is the final part of the series that started with... Callback and Multicast delegatesOne more E ...
- Python 管理 MySQL
Python MySQLdb 模块 Python pymysql 模块 Python SQLAlchemy 模块 Python ConfigParser 模块 Python 创建 MySQL 配置文件 ...
- PHP 函数引用传值
<?php /* * @1 $arr = array_fill(1,100,'bbb'); echo memory_get_usage()."<br>"; fun ...
- 学了Python可以做什么工作
学了Python可以做什么工作 用 Python 写爬虫 据我所知很多初学 Python 的人都是使用它编写爬虫程序.小到抓取一个小黄图网站,大到一个互联网公司的商业应用.通过 Python 入门爬虫 ...
- Android程序对不同手机屏幕分辨率自适应的方法
相信各位Android开发爱好者都知道,由于OEM之间的竞争,各种Android操作系统的手机简直就是琳琅满目,屏幕分辨率的差异可想而知.目前比较主流的有WVGA=800x480,HVGA=480x3 ...
- Geoserver
Geoserver是一个功能齐全,遵循OGC开放标准的开源WFS-T和WMS服务器.利用Geoserver可以把数据作为maps/images来发布(利用WMS来实现)也可以直接发布实际的数据(利用W ...
- 【Python系列】Python3获取控制台输入
""" 接收控制台的输入 How old are you? 18 How tall are you ? 180 How much do you weigh? 50 So ...
- android cannot locate symbol 'sigemptyset'问题解决
设备是android 4.1的平板电脑,支持armeabi-v7a和mips,为了能用上poco c++ lib,用cmake编译了poco mips架构的lib,但在android studio里引 ...
- LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...
- 3944: Sum[杜教筛]
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3471 Solved: 946[Submit][Status][Discuss] ...