题目: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. mini KMS Activator v1.3破解激活microsoft Office 2010 RTM

    利用mini KMS Activator v1.3破解激活microsoft Office 2010 RTM方法,只是为体验office而做测试使用的哦...大家觉得好就自觉购买正版去... 使用步骤 ...

  2. 今天在发布IIS站点的时候遇到了一些问题

    1.HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 分析:一般5XX的错误都是服务器的问题,这里把应用程序池 ...

  3. C语言入门(7)——自定义函数

    C源程序是由函数组成的.虽然在C语言入门系列前面几篇的程序中大都只有一个主函数main(),但实用程序往往由多个函数组成.函数是C源程序的基本模块,通过对函数模块的调用实现特定的功能.C语言中的函数相 ...

  4. String类的实现,内部采用字符数组实现

    #include <iostream> using namespace std; class String{ public: String(const char *str = NULL); ...

  5. 驾驶机动车在高速公路上倒车、逆行、穿越中央分隔带掉头的一次记6分。 答案:错误 2013《123号令-附件2》一、机动车驾驶人有下列违法行为之一,一次记12分[重新考《科目一》]:(七)驾驶机动车在高速公路上倒车、逆行、穿越中央分隔带掉头的; 可以参考:http://zhinan.jxedt.com/info/6375.htm

    这一组交通警察手势是什么信号?_2600602 交警的面部对着哪个方向就是在指挥哪个方向的车,减速慢行是右手   左转弯待转是左手!~ 哎 本题解释由台州交通驾校提供 4755支持   hmq 只能看 ...

  6. iOS_词典阵列 按key分组和排序

    // // main.m // SortGroup // // Created by beyond on 14-10-26. // Copyright (c) 2014年 beyond.com All ...

  7. java读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  8. VBA 开发学习--基础语法

    MsgBox "开始学习VBA" '提示框 Dim str As String '声明str变量是string类型 Let str = "一起来看流星雨" '给 ...

  9. Nutch

    nutch 插件开发[资料整理]:http://my.oschina.net/cloudcoder/blog/472915 Nutch2.3+Mongodb+ElasticSearch:http:// ...

  10. CentOS安装常用软件

    下载第三方库rpmforge,找到合适自己版本的rpmforge下载,用以支持NTFS格式硬盘和MP3格式音频或其他 http://pkgs.repoforge.org/rpmforge-releas ...