本题链接 : 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:

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output:

Line 1: One integer: the maximum of time any one cow must walk.
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 最短路径问题的更多相关文章

  1. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  2. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  3. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  4. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3268 Silver Cow Party 最短路径+矩阵转换

    Silver Cow Party Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  6. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

  9. poj 3268 Silver Cow Party

                                                                                                       S ...

随机推荐

  1. 【Hook技术】实现从"任务管理器"中保护进程不被关闭 + 附带源码 + 进程保护知识扩展

    [Hook技术]实现从"任务管理器"中保护进程不被关闭 + 附带源码 + 进程保护知识扩展 公司有个监控程序涉及到进程的保护问题,需要避免用户通过任务管理器结束掉监控进程,这里使用 ...

  2. <c:if>判断两个<c:forEach>里的数据是否相等

    问题:两个<c:forEach>嵌套,里面循环的值和外面的值进行比较(里层里的PARENTID是否等于外层的ID),如果相等就显示. <c:forEach items="$ ...

  3. 服务端套接字类CxServerSocket的使用

    服务端套接字类CxServerSocket的使用 这是一个精练的服务端套接字类,类名.函数名和变量名均采用匈牙利命名法.小写的x代表我的姓氏首字母(谢欣能),个人习惯而已,如有雷同,纯属巧合. CxS ...

  4. Use weechat (IRC client) on OS X. MacBook Pro

    Weechat is a console IRC client. It is opensource and very easy to use. I use weechat in my Linux PC ...

  5. NDepend 3.0已与Visual Studio集成

    NDepend 3.0已与Visual Studio集成 投递人 itwriter 发布于 2010-02-10 16:17 评论(0) 有1638人阅读  原文链接  [收藏]  « » NDepe ...

  6. vmstat命令查看系统资源占用情况

    vmstat是一个十分有用的Linux系统监控工具,使用vmstat命令可以得到关于进程.内存.内存分页.堵塞IO.traps及CPU活动的信息.可用以下命令查看: # vmstat 2 直接查看系统 ...

  7. [珠玑之椟]估算的应用与Little定律

    [珠玑之椟]估算的应用与Little定律 估算的数据主要依赖于所能获得的数据和常识,有时还包括实践而不仅仅是理论.它常常作为一个大问题中的子问题,恰当地估算可以省去精确计算的时间和开销.在计算机领域, ...

  8. Cygwin 各种情况下中文乱码--终极解决方案

    0.引言 本人从进公司以来一直负责公司Android平台下产品的NDK开发,用的工具: 01. Google的adt-bundle(集成了eclipse和sdk) 02. NDK 03. Cygwin ...

  9. QT 初试 MainWindow简易窗体

    1.创建一个空的QT工程文件 2 建立程序文件 MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QMainWind ...

  10. JVM方法调用栈

    摘自深入分析java web技术内幕