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 (求给定两点之间的最短路)的更多相关文章

  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. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

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

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

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

  8. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  9. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

随机推荐

  1. 【CentOS】Eclipse插件egit使用

    1.简介 2.安装 3.配置 4.使用 5.补充说明   参考资料: http://yufenfei.iteye.com/blog/1750124    1.简介     EGit就是一款Eclips ...

  2. eclipse中设置中文javadoc+如何查看class的中文javadoc

    一.  eclipse中设置中文javadoc 1.先到http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish ...

  3. JavaScript之substring()方法讲解

    定义和用法 substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数 描述 start 必需.一个非负 ...

  4. iOS 面试题

    1.Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答:不可以,可以实现多个接口:category是分类,,一般情况下分类 ...

  5. Best Practices for Web Apps

    Mobile Web Best Practices Exceptional Performance Let's make the web faster

  6. √新技能Get - 教你发空白朋友圈

    今天下午都被空白朋友圈刷屏了.空白朋友圈也即是在朋友圈里面发空消息,没有图片也没有文字,朋友圈动态是空空的.这是谁在恶搞呢?怎么实现呢? 怎么发空消息啊?其实这是为了帮助大家识别身边用iOS的小伙伴的 ...

  7. 超快的 FastText

    Word2Vec 作者.脸书科学家 Mikolov 文本分类新作 fastText:方法简单,号称并不需要深度学习那样几小时或者几天的训练时间,在普通 CPU 上最快几十秒就可以训练模型,得到不错的结 ...

  8. ZOJ3724 Delivery(树状数组??)

    题意:给你一个有向图,第一类边是从第i个点到第i+1个点的,还有多出来的m条二类边,是从u到v的,同样是有向的.然后你要处理询问,从u到v经过最多一次二类边的最短距离是多少. 题目我觉得是神题,然后看 ...

  9. POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)

    题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. #include <stdi ...

  10. 【hdu3065-病毒侵袭持续中】AC自动机

    题意:给定一些只含大写字母的病毒串,再给一个文本串,问文本串中每个病毒串各出现了多少次. 题解: 就是用AC自动机,在每个节点末尾有个id记录是哪个单词的末尾,然后如果同时是多个单词的末尾就用一个ne ...