poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 13611 | Accepted: 6138 |
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
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 题目及算法分析:
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define N 1000+20
#define INF 0x3f3f3f3f using namespace std; int map[N][N];
int dis[N], ans[N];
bool vis[N];
int n, m, s; int Dijkstra(int s)
{
int i, j, k;
for(i=1; i<=n; i++)
dis[i]=map[s][i];
vis[s]=true;
for(k=1; k<n; k++)
{
int mi=INF, pos;
for(i=1; i<=n; i++)
{
if(vis[i]==false && dis[i]<mi )
{
mi=dis[i]; pos=i;
}
}
vis[pos]=true;
for(j=1; j<=n; j++)
{
if(vis[j]==false && dis[j]>dis[pos]+map[pos][j] )
dis[j]=dis[pos]+map[pos][j];
}
}
for(i=1; i<=n; i++)
{
ans[i]=ans[i]+dis[i];
}
return 0;
} void turn_over()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<i; j++)
swap(map[i][j], map[j][i] );
}
} int main()
{
scanf("%d %d %d", &n, &m, &s);
int u, v, w; for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(i==j) map[i][j]=0;
else map[i][j]=INF;
} for(int i=0; i<m; i++)
{
scanf("%d %d %d", &u, &v, &w);
map[u][v] = w;
}
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
Dijkstra(s);
turn_over();
memset(vis, false, sizeof(vis));
Dijkstra(s);
int cc=-1;
for(int i=1; i<=n; i++)
{
if(ans[i]>cc && ans[i]<INF )
cc=ans[i];
}
printf("%d\n", cc );
return 0;
}
poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )的更多相关文章
- POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)
题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...
- 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 (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 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 (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- jQuery ajax 获取信息展示在“下拉列表”中
<link href="${ctxStatic}/jquery-select2/4.0.3/select2.min.css" rel="stylesheet&quo ...
- vuex 深入理解
参考自:https://mp.weixin.qq.com/s?src=11×tamp=1528275978&ver=922&signature=ZeHPZ2ZrLir ...
- dos下连接mysql,显示表结构
C:\Windows\system32>mysql -hlocalhoset -uroot -p Enter password: ***** mysql> use ssh Database ...
- 【转载】ASP.Net WebForm温故知新学习笔记:一、aspx与服务器控件探秘
开篇:毫无疑问,ASP.Net WebForm是微软推出的一个跨时代的Web开发模式,它将WinForm开发模式的快捷便利的优点移植到了Web开发上,我们只要学会三步:拖控件→设属性→绑事件,便可以行 ...
- 修改Oracle SGA,以提高oracle性能
在正常情况下,查询非常慢. 1.检查SGA大小,以DBA身份连接到oracle数据库,输入show sga. 2.如果SGA过小,请修改其大小 修改SGA必须保持的原则 1).sga_target不能 ...
- 一条长l的笔直的街道上有n个路灯,若这条街的起点为0,终点为l,第i个路灯坐标为ai,每盏灯可以覆盖到的最远距离为d,为了照明需求,所有灯的灯光必须覆盖整条街,但是为了省电,要是这个d最小,请找到这个最小的d。
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
- nginx - KeepAlive详细解释
最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通过Client访问的是LVS的VIP, VIP后端挂载的RealServer是Nginx服务器. Client可以是浏览器 ...
- Ubuntu 12.04中文输入法的安装(zhuan)
Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu ...
- Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现
看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...
- Android Thread.UncaughtExceptionHandler异常消息捕获
public void uncaughtException(Thread thread, Throwable ex) { //处理异常 Log.e("崩溃",thread.getN ...