题目大意:

有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间。

题目分析:

其实就是以X为终点,求出X到其他每个点的距离, 再将图反存一下,在做一次最短路, 两次距离相加求出最长的时间。

这里是用Dijkstra写的,我们第一次用邻接矩阵写,第二次用邻接表,并且有优先队列优化

 #include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 1005
int G[][maxn][maxn];
int dist[][maxn];
bool vis[][maxn];
int n, m, X;
void Dijkstra(int Star,int End,int k)
{
dist[k][Star] = ; for(int i=; i<=n; i++)
{
int Min = INF, index;
for(int j=; j<=n; j++)
{
if(dist[k][j] < Min && !vis[k][j])
Min = dist[k][j], index = j;
} vis[k][index] = true; for(int j=; j<=n; j++)
{
if(!vis[k][j])
dist[k][j] = min(dist[k][j], dist[k][index] + G[k][index][j]);
}
}
}
void Init()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)
{
dist[][i] = dist[][i] = INF;
for(int j=; j<=n; j++)
G[][i][j] = G[][j][i] = G[][i][j] = G[][j][i] = INF;
}
} int Slove()
{
int Max = ;
Dijkstra(X,n,);
Dijkstra(X,n,);
for(int i=; i<=n; i++)
{
Max = max(dist[][i]+ dist[][i], Max);
}
return Max;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&X) != EOF)
{
Init();
for(int i=; i<m; i++)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
G[][a][b] = min(G[][a][b],c);
G[][b][a] = min(G[][b][a],c);
}
int ans = Slove(); cout << ans << endl;
}
return ;
}

优先队列版本

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 1006
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
struct Edge
{
int e;
int w;
friend bool operator < (Edge n1, Edge n2)
{
return n1.w > n2.w;
}
};
vector<Edge>G[2][maxn]; int dist[2][maxn];
bool vis[2][maxn];
int n, m, X;
void Dijkstra(int Star,int End,int k)
{
Edge P, Pn;
P.e = Star;
P.w = 0;
dist[k][P.e] = 0;
priority_queue<Edge> Q;
Q.push(P); while( !Q.empty() )
{
P = Q.top();
Q.pop(); if( vis[k][P.e] )
continue; vis[k][P.e] = true; int len = G[k][P.e].size(); for(int i=0; i<len; i++)
{
Pn.e = G[k][P.e][i].e;
Pn.w = G[k][P.e][i].w + P.w;
if( !vis[k][Pn.e] )
{
dist[k][Pn.e] = min(dist[k][Pn.e],Pn.w);
Q.push(Pn);
}
}
}
}
void Init()
{
memset(vis,false,sizeof(vis));
for(int i=0; i<=n; i++)
{
G[0][i].clear();
G[1][i].clear();
dist[0][i] = dist[1][i] = INF;
}
} int Slove()
{
int Max = 0;
Dijkstra(X,n,0);
Dijkstra(X,n,1);
for(int i=1; i<=n; i++)
{
Max = max(dist[0][i]+ dist[1][i], Max);
}
return Max;
}
int main()
{
Edge P;
while(scanf("%d%d%d",&n,&m,&X) != EOF)
{
Init();
for(int i=0; i<m; i++)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
P.e = b, P.w = c;
G[0][a].push_back(P);
P.e = a;
G[1][b].push_back(P);
}
int ans = Slove(); cout << ans << endl;
}
return 0;
}

  

POJ 3268 Silver Cow Party ( Dijkstra )的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

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

  2. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  3. POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

    题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...

  4. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接: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 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

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

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

  7. POJ 3268 Silver Cow Party(最短路&Dijkstra)题解

    题意:有n个地点,有m条路,问从所有点走到指定点x再走回去的最短路中的最长路径 思路:用Floyd超时的,这里用的Dijkstra. Dijkstra感觉和Prim和Kruskal的思路很像啊.我们把 ...

  8. poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)

    题目 Dijkstra,正反两次最短路,求两次和最大的. #define _CRT_SECURE_NO_WARNINGS //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...

  9. POJ 3268 Silver Cow Party(dij+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<algorithm> #include<cs ...

随机推荐

  1. hdu 1882 Strange Billboard(位运算+枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1882 感觉非常不错的一道题. 给一个n*m(1<=n,m<=16)的矩阵,每一个格子都有黑白两面,当 ...

  2. MaxReceivedMessageSize :已超过传入消息(65536)的最大消息大小配额

    做的windows应用程序(后台调用webservice),数据量大的时候,报错如下: System.ServiceModel.CommunicationException: 已超过传入消息(6553 ...

  3. Linux中date命令的各种实用方法--转载

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,自己也曾经为时间的各种表示方法和如何修改时间而困惑,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的 ...

  4. Python之路,Day26-----暂无正在更新中

    Python之路,Day26-----暂无正在更新中

  5. VB编程技巧推荐

    VB编程技巧推荐   1.zyl910的专栏——理论水平高 用VB写高效的图像处理程序 V2.0 优化分支代码——避免跳转指令堵塞流水线 2.Laviewpbt的专栏 —— 有很多算法的代码,实用性高 ...

  6. Web api 文档以及测试工具配置

    第一步: 创建web api 在nuget 上搜索 webapitestclient (包含预发行版) 然后在 /Areas/HelpPage/Views/Help/Api.cshtml 末尾 添加 ...

  7. java 跳转地址栏地址改变

    在strtus1 中,很多都是直接的action 配置后进行跳转的 这样地址栏是不会改变的 如果需要进行浏览器跳转 ActionForward actionForward = new ActionFo ...

  8. HTML 学习笔记

    1HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. 并且只有这6种标题,标题中加多个空格,和一个空格没区别,标题文字前后加默认空格会被去除. ...

  9. Mysql 列转行group_concat函数,与行转列

    1.正常情况. SELECT JoinEventIds from nt_mainnum 2.使用group_concat函数 select group_concat(JoinEventIds) fro ...

  10. CSS中表示cellpadding和cellspacing的方法

    本文和大家重点讨论一下用CSS来表示表格的cellpadding和cellspacing方法,表格的cellpadding和cellspacing我们经常会用一定的方式来清除默认样式,请看下文详细介绍 ...