题目链接:

https://vjudge.net/problem/POJ-3268

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 ≤ XN). 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: N, M, and X
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

Line 1: One integer: the maximum of time any one cow must walk.

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

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
 /*
关键是反向存储,求最短路的思维转换
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
void Dijkstra(int s,int e[][]);
#define inf 99999999
int dis[],book[],e[][],f[][],d[],n,m;
int main()
{
int i,j,k,x,a,b,c,maxn,temp;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
memset(d,,sizeof(d));
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i==j)
{
e[i][j]=;
f[i][j]=;
}
else
{
e[i][j]=inf;
f[i][j]=inf;
} for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c<e[a][b])
{
e[a][b]=c;
f[b][a]=c;
}
}
Dijkstra(x,e);//回
Dijkstra(x,f);//去
maxn=-;
for(i=;i<=n;i++)
{
if(d[i]>maxn)
maxn=d[i];
}
printf("%d\n",maxn);
}
return ;
}
void Dijkstra(int s,int e[][])
{
int i,j,k,min,u;
for(i=;i<=n;i++)
dis[i]=e[s][i];
memset(book,,sizeof(book));
book[s]=;
for(k=;k<n;k++)
{
min=inf;
for(i=;i<=n;i++)
if(book[i]==&&dis[i]<min)
{
min=dis[i];
u=i;
}
book[u]=;
for(i=;i<=n;i++)
if(book[i]==&&dis[i]>dis[u]+e[u][i])
dis[i]=dis[u]+e[u][i];
}
for(i=;i<=n;i++)
d[i]+=dis[i];
}

POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

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

  2. POJ 3268 Silver Cow Party (Dijkstra + 优先队列)

    题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是 ...

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

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

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

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

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

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

  6. 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 bi ...

  7. POJ 3268 Silver Cow Party 最短路

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

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

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

  9. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

随机推荐

  1. ABP框架系列之九:(Abp-Session-会话)

    Introduction ASP.NET Boilerplate provides IAbpSession interface to obtain current user and tenant wi ...

  2. python repr()和str()

    两者功能差不多,都是把对象转为字符串表示形式,但是也有区别,repr()之后再eval()可以转为原型,但str()只能保证大多数,不能100% 其中主要的 差别在与 字符串对象本身,比如 a = ' ...

  3. shell解析my.cnf配置文件

    my.cnf配置格式如下 vi my.cnf[client]port=3306socket=/tmp/mysql.socket [mysqld]port=3306server-id=1datadir= ...

  4. 20155326刘美岑 《网络对抗》Exp1 PC平台逆向破解

    20155326刘美岑 <网络对抗>逆向及Bof基础实践 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函 ...

  5. Navicat for MYSQL 断网时本地连接无法打开,2005错误

    Navicat for MYSQL 断网时本地连接无法打开,2005错误 NO1 提示下图: NO2 解决方法: (1)选中本地连接,右键 连接属性 (2) 将 主机名或IP地址 这一栏改为 127. ...

  6. Android-Java-静态成员变量&成员变量&局部变量(内存图&回收机制)

    静态成员变量(回收机制) StaticDemo 和 MyDemo package android.java.oop13; class MyDemo { /** * 定义一个静态变量 */ public ...

  7. 一个隐蔽的C语言问题反思

    今天在编译一个C代码的时候,从别的编译ok的头文件中拷贝了一份在上面做修改,没想到修改好之后一直 无法调用这个头文件中的函数和变量.看了好久,才在预编译宏中找到了问题的根源.代码 如下所示: 头文件A ...

  8. OC学习5——类和对象

    1.OC是在C语言基础上进行扩展得到的一门面向对象的程序设计语言,它也提供了定义类.成员变量和方法的基本功能.类可以被认为是一种自定义的数据类型,使用它可以定义变量,所有使用类定义的变量都是指针类型的 ...

  9. [源码]K8 Cscan模块 C#获取内网主机IP/机器名/Banner/网页标题源码

    [原创]K8 Cscan 大型内网渗透自定义扫描器 https://www.cnblogs.com/k8gege/p/10519321.html Cscan简介:何为自定义扫描器?其实也是插件化,但C ...

  10. C# datagridview分页功能

    winform开发是或多或少都会接触datagridview控件,如果数据量大,那么必须使用分页功能,但是datagridview自身并没有分页,所以我们要自己实现.在网上搜了一些发现没有太适合自己的 ...