题目大意:

有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. gcc -I -L -l区别

    我们用gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数,下面做个记录: 例: gcc -o hello hello.c -I /home/hello/inc ...

  2. 再回首,Java温故知新(七):Java基础之运算符

    常规算数运算符 包括+.-.*./.%分别对应着加.减.乘.除.取余,算数运算符所处理的两个数字必须为同种类型,如果是不同类型且无法进行自动类型转换的话必须进行强制类型转换,不过强制类型转换可能会丢失 ...

  3. ios中xib的使用介绍

    ios中Xib的使用 ios中xib的使用 Nib files are the quintessential(典型的) resource type used to create iOS and Mac ...

  4. DataTable数据与Excel表格的相互转换

    using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...

  5. PHP 网页爬虫

    只能爬一个页面 <?php function get_urls($url){ $url_array=array(); $the_first_content=file_get_contents($ ...

  6. 黑马程序员-for和foreach

    class Program { static void Main(string[] args) { Console.WriteLine("***第一种情况****************** ...

  7. 计时器(Chronometer)的使用

    安卓提供了一个计时器组件:Chronometer,该组件extends TextView,因此都会显示一段文本,但是它显示的时间是从某个起始时间开始过去了多少时间,它只提供了android:forma ...

  8. 64位Window操作系统下,Orcal数据访问服务器端和客户端版本对应与通讯问题

    最近做一个小系统,需要在客户现场搭建数据库环境.之前我们一直访问的是公司的一个测试库,现在需要在现场开发,现场的Orcal服务器是12C ,我们本不打算重装服务器端orcal,故将我们自己电脑的orc ...

  9. 使用<span>标签为文字设置单独样式

    这一小节讲解<span>标签,我们对<em>.<strong>.<span>这三个标签进行一下总结: 1. <em>和<strong& ...

  10. 为什么struts2 ajax 方法执行两次

    struts2中使用json插件执行ajax处理时,如果方法名是get方法的时候,方法会莫名其妙的执行两次. 原因: struts2 中JSON的原理是在ACTION中的get方法都会序列化,所以前面 ...