poj - 3268 Silver Cow Party (求给定两点之间的最短路)
http://poj.org/problem?id=3268
每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的农场,但是他们都想选一条最近的路,并且由于路是单向的,去的路和来的路选择可能不一样,问来去时间之和最大是多少?
这题等于给定了起点和终点,那么求出(d[x][i]+d[i][x])最大的那个即可。
开始错了几次,太不小心了,就是最后求最大值的时候,用了一个临时变量没置0,所以可能会导致错误。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn= ;
const int INF = <<;
struct edge{
int to,cost;
edge(){}
edge(int x,int y) {
to=x;
cost=y;
}
};
typedef pair<int,int>P; int N,M,X;
vector<edge>G[maxn];
int d[maxn]; int dijkstra(int s,int t) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=;i<=N;i++) d[i]=INF;
d[s]=;
que.push(P(,s)); while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(v==t) return d[t];
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b,c;
while(~scanf("%d%d%d",&N,&M,&X)) {
for(int i=;i<M;i++) {
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(edge(b,c));
}
int sum=;
for(int i=;i<=N;i++) {
sum=max(sum,dijkstra(i,X)+dijkstra(X,i));
}
printf("%d\n",sum);
}
return ;
}
还有一种方法,首先求出点X到各个点i的最短路径,这就是奶牛返回的最短距离,然后把路径反向,在求点X到各个点i的最短距离,就是每个点到点X的最短距离。
最后把两个距离相交即可。
不过对于反向,不是很明白。
时间直接从600多ms降到60多ms。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn= ;
const int INF = <<;
struct edge{
int to,cost;
edge(){}
edge(int x,int y) {
to=x;
cost=y;
}
};
typedef pair<int,int>P; int N,M,X;
vector<edge>G[maxn],RG[maxn];
int d[maxn],rd[maxn]; void dijkstra(int s) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=;i<=N;i++) d[i]=INF;
d[s]=;
que.push(P(,s)); while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b,c;
scanf("%d%d%d",&N,&M,&X);
for(int i=;i<M;i++) {
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(edge(b,c));
RG[b].push_back(edge(a,c)); //存储 反向边
}
dijkstra(X); //第一次 dijkstra 是求X到各个点的距离
//for(int i=1;i<=N;i++) printf("%d\n",d[i]);
for(int i=;i<=N;i++) G[i]=RG[i]; //然后 把反向存储的边赋值给 G
// for(int i=1;i<=N;i++) {
// for(int j=0;j<G[i].size();j++)
// cout<<G[i][j].to<<" "<<G[i][j].cost<<endl;
//}
memcpy(rd,d,sizeof(d)); //把第一次的 最短距离 保存 dijkstra(X); //反向的 dijkstra 求出每个点到X的最短距离
//for(int i=1;i<=N;i++) printf("%d\n",d[i]);
int sum=; //枚举最大和
for(int i=;i<=N;i++) {
sum=max(sum,d[i]+rd[i]);
}
printf("%d\n",sum);
return ;
}
poj - 3268 Silver Cow Party (求给定两点之间的最短路)的更多相关文章
- 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 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- 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 最短路
原题链接: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 ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
随机推荐
- Matlab中颜色线形和形状
1.颜色字符串有'c', 'm', 'y', 'r', 'g', 'b', 'w',和'k'.分别表示青,红紫,黄,红,绿,白和黑. 2.线型字符串有:'-' 为实线, '--' 为虚线, ':' 为 ...
- Matlab实现网络拓补图
顶点号 顶点号 权值 1 2 400 1 3 450 2 4 300 2 8 230 2 9 140 3 4 600 4 5 210 4 19 310 5 6 230 5 7 200 6 7 320 ...
- Java内存模型(JMM)
参考: 1. http://www.tuicool.com/articles/UVzuQb
- LoaderManager使用详解(一)---没有Loader之前的世界
来源: http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html 感谢作者Alex ...
- JUC回顾之-ThreadPoolExecutor的原理和使用
Spring中的ThreadPoolTaskExecutor是借助于JDK并发包中的java.util.concurrent.ThreadPoolExecutor来实现的.基于ThreadPoolEx ...
- java 反射创建对象并传入参数
/* * 通过反射创建带参数的对象 */ public Object Creatobject(String ClassPath, Object[] Params) throws Exception { ...
- Python 融于ASP框架
一.ASP的平反 想到ASP 很多人会说 “asp语言很蛋疼,不能面向对象,功能单一,很多东西实现不了” 等等诸如此类. 以上说法都是错误的,其一ASp不是一种语言是 微软用来代替CGI的一种web框 ...
- Xamarin for Visual Studio 3.11.666 Beta版 破解补丁
注意:本版本是 Beta 版 现已推送到稳定频道 前提概要 全新安装请参考 安装 Xamarin for Visual Studio. 最新稳定版请参考 Xamarin for Visual St ...
- uva 12589 - Learning Vector
思路: 容易知道加向量的顺序是按向量斜率的大小顺序来的.由于数据不是很大,可以用背包解决!! dp[i][j]:加入最大面积为i时,加入了j个向量. 代码如下: #include<iostrea ...
- poj 3522(最小生成树应用)
题目链接:http://poj.org/problem?id=3522思路:题目要求最小生成树中最大边与最小边的最小差值,由于数据不是很大,我们可以枚举最小生成树的最小边,然后kruskal求最小生成 ...