Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15989   Accepted: 7303

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 irequires 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.

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.

求目标点到图中的其他点来回的最小值。

Dijkstra直接求来回的距离,然后比较求出最小值。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; const int MAX = 100005;
int edge[1005][1005];
int vist[1005],vist2[1005],minidis1[1005][1005],minidis2[1005][1005];
int N,M,X; void init()
{
int i,j; for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
if(j==i)
{
edge[i][j]=0;
minidis1[i][j]=0;
minidis2[i][j]=0;
}
else
{
edge[i][j]=-1;
minidis1[i][j]=MAX;
minidis2[i][j]=MAX;
}
}
}
for(i=1;i<=N;i++)
{
vist[i]=0;
vist2[i]=0;
}
} void dijkstra(int i)
{
int j,k;
int position=i;
int position2=i; vist[position]=1;
vist2[position]=1;
minidis1[i][position]=0;
minidis2[position][i]=0; for(j=1;j<=N-1;j++)//一共要添加进num-1个点
{
for(k=1;k<=N;k++)
{
if(vist[k]==0 && edge[position][k]!=-1 && minidis1[i][position]+edge[position][k] < minidis1[i][k])//新填入的点更新minidis
{
minidis1[i][k]=minidis1[i][position]+edge[position][k];
}
if(vist2[k]==0 && edge[k][position2]!=-1 && minidis2[position2][i]+edge[k][position2] < minidis2[k][i])//新填入的点更新minidis
{
minidis2[k][i]=minidis2[position2][i]+edge[k][position2];
}
}
int min_value=MAX,min_pos=0;
int min_value2=MAX,min_pos2=0;
for(k=1;k<=N;k++)
{
if(vist[k]==0 && minidis1[i][k]<min_value)//比较出最小的那一个作为新添入的店
{
min_value = minidis1[i][k];
min_pos = k;
}
if(vist2[k]==0 && minidis2[k][i]<min_value2)//比较出最小的那一个作为新添入的店
{
min_value2 = minidis2[k][i];
min_pos2 = k;
}
} vist[min_pos]=1;
position=min_pos; vist2[min_pos2]=1;
position2=min_pos2;
} } int main()
{
int i;
cin>>N>>M>>X;
init(); int temp1,temp2,temp3;
for(i=1;i<=M;i++)
{
cin>>temp1>>temp2>>temp3;
edge[temp1][temp2]=temp3;
}
memset(vist,0,sizeof(vist));
memset(vist2,0,sizeof(vist2)); dijkstra(X);
int ans=-1;
for(i=1;i<=N;i++)
{
if(i==X)continue;
ans=max(ans,minidis1[X][i]+minidis2[i][X]);
} cout<<ans<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3268:Silver Cow Party 求单点的来回最短路径的更多相关文章

  1. poj - 3268 Silver Cow Party (求给定两点之间的最短路)

    http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...

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

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

  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 (双向dijkstra)

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

  5. POJ 3268 Silver Cow Party 最短路

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

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

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

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

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

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

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

  9. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

随机推荐

  1. SqlService 并发测试

    使用Sql QueryStress 可输入需要的线程数量,执行次数,对SQL 语句或存储过程进行测试,可查看执行时间及资源耗用.

  2. JDBC连接MySql例子

    1.注册MySql连接驱动 2.设置连接MySql连接字符串.用户名和密码 3.获取数据库连接 代码如下: // 加载驱动 Class.forName("com.mysql.jdbc.Dri ...

  3. 2019年springmvc面试高频题(java)

    前言 2019即将过去,伴随我们即将迎来的又是新的一年,过完春节,马上又要迎来新的金三银四面试季.那么,作为程序猿的你,是否真的有所准备的呢,亦或是安于本职工作,继续做好手头上的事情. 当然,不论选择 ...

  4. Flask程序相关配置加载的三种方式

    方式一:从对象中加载配置 1.定义配置类,在配置类中添加相应的配置 2.通过app.config.from_object(配置类)进行加载 代码如下: from flask import Flask ...

  5. 名称空间using namespace std

    写再最前面:摘录于柳神的笔记 虽然C++是一门面向对象语言,但是对于刷算法这件事而言,我们并不需要掌握它⾯向对象的部分-只 需要掌握刷算法的时候需要用到的部分(基本输⼊输出.STL标准模板库. str ...

  6. tensorflow中的图(02-1)

    由于tensorflow版本迭代较快且不同版本的接口会有差距,我这里使用的是1.14.0的版本 安装指定版本的方法:pip install tensorflow==1.14.0      如果你之前安 ...

  7. LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

    题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...

  8. FiBiNET-学习

    Our main contributions are listed as follows: • Inspired by the success of SENET in the computer vis ...

  9. 「SCOI2005」王室联邦

    「SCOI2005」王室联邦 传送门 树分块. 考虑把树,按照节点个数每 \(B\) 个分块,把块顶作为省会. 这是具体证明 参考代码: #include <cstdio> #define ...

  10. python3 使用selenium +webdriver打开chrome失败,报错:FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

    提示chrome driver没有放置在正确的路径下 解决方法: 1.chromedriver与chrome各版本及下载地址 驱动的下载地址如下: http://chromedriver.storag ...