题目: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. 使用TWebBrowser组件保存网页为html和mht文件 收藏

    一.保存为HTML文件 uses ActiveX;...procedure WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ;var ...

  2. [原]基于CAS实现单点登录(SSO):登录成功后,cas client如何返回更多用户信息

    从cas server登录成功后,默认只能从casclient得到用户名.但程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况. cas client拿到用户名后再到数据库中查 ...

  3. centos6 qt ENV

    打算做嵌入式图像处理,计划方案嵌入式Linux+OpenCV+QT,昨天简单入门OpenCV今天看看QT,QT就先弄Linux下面的,回家之前争取把基本的摸通,然后能在板子上跑起来 关于QT安装 QT ...

  4. 删除select中所有option选项

    这样写 <select id="search"> <option>baidu</option> <option>sogou</ ...

  5. SICP 练习 (2.9)解决摘要:宽度和区间运算的关系间隔

    SICP 2.9 像是一个数学题,要我们证明区间的和与差的宽度是被加和被减的区间的宽度的函数,而对于乘法和除法来说不成立. 书中所谓宽度就是区间起点和终点差的一半.以我看来更像是区间宽度的一半.无论怎 ...

  6. ObjectiveC中的block用法解析

    Block Apple 在C, Objective-C,C++加上Block这个延申用法.目前只有Mac 10.6 和iOS 4有支持.Block是由一堆可执行的程序组成,也可以称做没有名字的Func ...

  7. Heritrix个性化设置抓取目标

    本文是Heritrix的使用的高级篇,针对对Heritrix已经能够运行的码农朋友们! 我们在抓取网页的时候,网页的链接中往往会包含有js.css.图片.视频等文件,第一次执行抓取任务的时候,许多农民 ...

  8. 使用x manager 连接Linux桌面

    /usr/bin/xterm -ls -display $DISPLAY 需要安装xterm 服务

  9. oracle rac 安装脚本

    1. 配置/etc/hosts 网络 192.168.1.111 rac1 rac1.oracle.com192.168.1.182 rac1-vip 192.168.1.222 rac2 rac2. ...

  10. 消息处理之performSelector

    performSelector和直接调用方法的区别 performSelector: withObject:是在iOS中的一种方法调用方式.他可以向一个对象传递任何消息,而不需要在编译的时候声明这些方 ...