题目:click here

题意:

  给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出。

分析:

  最短路径只需要从x到i的最短路径代表他们返回的最短路径,然后将所有边反过来,再从x到i的最短路径代表他们来参加聚会的最短路径,这样对应相加找出一个最大值就可以了,当然其实不需要将所有边反过来,只需要将map的行和列对换一下就可以了,数据比较大,所以floyd超时,用dijkstra比较好点。

邻接矩阵:~~~不知道这个题为什么邻接矩阵会比邻接表快~~~

 #include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm> using namespace std;
typedef pair<int,int> P;
const int INF = 1e9+;
const int M = 1e3+; int n, m, x;
int f, t, c;
int cost[M][M];
int costb[M][M];
int d[M];
int db[M];
bool vis[M]; void dijkstra( int s, int *dist, int field[M][M] ) {
fill( dist, dist+n+, INF );
memset( vis, false, sizeof(vis) );
vis[s] = true;
dist[s] = ;
int tm = n;
while( tm-- ) {
int minn = INF, k = s;
for( int i=; i<=n; i++ ) {
if( !vis[i] && dist[i] < minn ) {
minn = dist[i];
k = i;
}
}
vis[k] = true;
for( int i=; i<=n; i++ ) {
if( !vis[i] && dist[i] > dist[k] + field[k][i] )
dist[i] = dist[k] + field[k][i];
}
}
} void solve() {
int ret = -INF;
dijkstra( x, d, cost ); // 正图 和 反图的最短路
dijkstra( x, db, costb );
for( int i=; i<=n; i++ )
ret = max( ret, d[i]+db[i] );
printf("%d\n", ret );
} int main() {
while( ~scanf("%d%d%d", &n, &m, &x ) ) {
for( int i=; i<=n; i++ ) {
for( int j=; j<=n; j++ ) {
cost[i][j] = INF;
costb[i][j] = INF;
}
}
for( int i=; i<m; i++ ) {
scanf("%d%d%d", &f, &t, &c );
cost[f][t] = c;
costb[t][f] = c;
}
solve();
}
return ;
}

邻接表:

 #include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm> using namespace std;
typedef pair<int,int> P;
const int INF = 1e9+;
const int M = 1e3+; struct edge { int to, cost; };
int n, m, x;
int f, t, c;
int d[M];
int db[M];
vector<edge> G[M]; void dijkstra( int s, int *dist ) {
priority_queue< P, vector<P>, greater<P> > que;
fill( dist, dist+n+, INF );
dist[s] = ;
que.push( P(,s) );
while( !que.empty() ) {
P p = que.top(); que.pop();
int v = p.second;
if( dist[v] < p.first ) continue;
for( int i=; i<G[v].size(); i++ ) {
edge e = G[v][i];
if( dist[e.to] > dist[v] + e.cost ) {
dist[e.to] = dist[v] + e.cost;
que.push( P(dist[e.to],e.to) );
}
}
}
} void solve() {
dijkstra( x, db );
int ret = -INF;
for( int i=; i<=n; i++ ) {
dijkstra( i, d );
ret = max( ret, d[x]+db[i] );
}
printf("%d\n", ret );
}
int main() {
while( ~scanf("%d%d%d", &n, &m, &x ) ) {
for( int i=; i<m; i++ ) {
scanf("%d%d%d", &f, &t, &c );
G[f].push_back( (edge){t,c} );
}
solve();
}
return ;
}

POJ 3268 Silver Cow Party 正反图最短路的更多相关文章

  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——————【最短路、Dijkstra、反向建图】

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

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

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

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

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

  8. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

  9. Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵

    Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...

随机推荐

  1. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  2. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

  3. [Ext JS 4] Grid 实战之分页功能

    前言 分页功能的实现有两种途径: 一种是服务端分页方式, 也就是web客户端传递页码参数给服务端,服务端根据页面参数返回指定条数的数据.也就是要多少取多少.这种方式比较适合Grid  的数据量很大,需 ...

  4. C++重载运算符的规则

    (1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载. 例如,有人觉得BASIC中用“* *”作为幂运算符很方便,也想在C++中将“* *”定义为幂运算符,用“3* *5”表示3 ...

  5. android 强制设置横屏 判断是横屏还是竖屏

    判断activity 是横屏还是竖屏  方法 1: //根据设备配置信息 Configuration cf= this.getResources().getConfiguration(); //获取设 ...

  6. PostMessage和SendMessage的区别

    1, PostMessage只把消息放入队列,不管其他程序是否处理都返回,然后继续执行,这是个异步消息投放函数.而SendMessage必须等待其他程序处理消息完了之后才返回,继续执行,这是个同步消息 ...

  7. Process Node.js 进程

    Process 进程 process.argv 是命令行参数数组,第一个元素是node,第二个元素是脚本文件名,从第三个元素开始每个元素是一个运行参数. process.stdout 标准输出流 co ...

  8. 使用MyBatis搭建一个访问mysql数据库的简单示例

    MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用 ...

  9. mysql alter example

    alter table `user_movement_log` AFTER `Regionid` (在哪个字段后面添加) ) default null; //主键 ) unsigned not nul ...

  10. Linux下Apache重启遇到No space left on device错误的解决方法

      解决办法:1.输入:ipcs -s 看有没有超过5个,如果有请执行下面2的命令:2.ipcs -s | perl -ane '/^0x00000000/ && `ipcrm -s ...