<poj - 3268> Silver Cow Party 牛のpart 最短路径问题
本题链接 : http://poj.org/problem?id=3268
题目大意:牛们要去聚会,输入N = 顶点数(牛场);M = 边(路)的数目; X = 终点 (聚会点)。问题:求来回时间的最大值。
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 Ti time units to traverse.
Output:
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
解题思路:因为本题是单向的,所以在做的时候可以做一个正向图和一个反向图,分别求解来回的时间,然后找和的最大值。
这里是我的代码:
#include <cstring>
#include <iostream>
#define INF 9999999
using namespace std; bool used[];
int V, E;
const int maxn = ; void dijkstra (int s, int cost[][], int d[]) {
fill (d, d + V + , INF);
fill (used,used + V + ,false);
d[s] = ;
while (true) {
int v = -;
for (int u = ; u <= V; u++) {
if (!used[u] && (v == - || d[u] < d[v])) v = u;
}
if (v == -) break;
used[v] = true;
for (int u = ; u <= V; ++u) {
if (d[u] > d[v] + cost[v][u]) {
d[u] = d[v] + cost[v][u];
}
}
}
} int cost[maxn][maxn];
int rcost[maxn][maxn]; int main () {
int d[maxn];
int rd[maxn];
int x, y, w;
int sum[maxn];
int S;
cin >> V >> E >> S; for (int i = ;i <= V; ++i)
for (int j = ; j <= V; ++j)
rcost[i][j] = cost[i][j] = INF; for (int i = ; i < E; ++i) {
cin >> x >> y >> w;
rcost[y][x] = cost[x][y] = w;
} dijkstra(S, cost, d);//分别计算最短路径
dijkstra(S, rcost, rd); for (int i = ; i <= V; ++i)//求和
sum[i] = d[i] + rd[i]; int maxnum = sum[];
for (int i = ; i <= V; ++i)///找最大值
if (sum[i] > maxnum)
maxnum = sum[i]; cout << maxnum << endl; return ;
}
欢迎码友评论,一起成长。
<poj - 3268> Silver Cow Party 牛のpart 最短路径问题的更多相关文章
- 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算法的优化。
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 : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- poj 3268 Silver Cow Party
S ...
随机推荐
- Unity3d物体模型(实现旋转缩放平移自动旋转)
基本功能实现:物体通过鼠标左键上下移动,中间键缩放.右键旋转,30秒没操作,物体自动旋转 实例代码: using UnityEngine; using System.Collections; publ ...
- 老鸟都应该注意的git 提交规范
不知道大家有没有看过自己项目的git 提交信息-----我看过好多次 ,不忍直视 然后提醒一起的小伙伴 :大家规范点 信息要详细, 过段时间再看下 ,还是一样. 相信很多猿都有这样的感受,对于垃圾的 ...
- jquery中each用法
通用遍历方法,可用于遍历对象和数组.$().each(),回调函数拥有两个参数: 第一个为对象的成员或数组的索引,第二个为对应变量或内容.如需退出each循环可使回调函数返回false 现有如下两个s ...
- 使用 App Studio 快速定制一个你自己的专属应用
使用 App Studio 快速定制一个你自己的专属应用 如果已有做一个手机应用的想法,只需要一些简单的图片,视频,或者RSS源,就可以通过App Studio制作出你心仪的应用! App Studi ...
- WP8开发札记(一)WP8应用生命周期管理
在介绍生命周期前,我们先了解两个相关的概念. 1.墓碑机制:WP8与Android采用的真后台机制不同,WP8采用的是墓碑机制.一旦从当前应用程序离开(非退出),该应用会被墓碑化,这样可以更好的管理( ...
- enode框架step by step之框架要实现的目标的分析思路剖析1
enode框架step by step之框架要实现的目标的分析思路剖析1 enode框架系列step by step文章系列索引: 分享一个基于DDD以及事件驱动架构(EDA)的应用开发框架enode ...
- android 源码编译 问题 列表
转自:http://www.cnblogs.com/xilinch/archive/2013/04/02/2996359.html make: *** [out/host/linux-x86/obj/ ...
- svn的使用详细说明
转载请指明:杨凯专属频道 1.在电脑的任意位置创建一个版本库文件夹 最好是全英文的目录 例如 在c:\svn下面创建一个 2.打开此文件,在这里右键 找--TortoiseSVN--->Crea ...
- GridControl的单元格中以buttonEdit实现文字和图片按钮并存的效果
话不多说,先上效果图 对于第一列的效果是如何实现的就不多说了,网上有很多例子 重点是第三列的效果实现方法,代码如下 private void GridSet() { DevExpress.XtraEd ...
- 简单的HTML注册界面
<html><head><title>我的第一个HTML页面</title></head><body><center> ...