POJ 3268-Silver Cow Party(dijkstra算法)

题目大意:给出一个单向带权图和一个点s,求点u,u到s的最短路径和s到u的最短路径之和最大。

对于s到任意点的最短路,直接dijkstra可以求出。

对于任意点到s的最短路,如果将所有边反向然后求一次最短路,容易证明,求出的s到任意点v的最短路s-->v就是原来没有反向的图的v-->s的最短路。

所以先求一次dijkstra,保存此次的结果,然后把所有的边反向,权值不变,再求一次dijkstra,将两次结果加起来,计算它们之中的最大值。

#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1010;
const int INF=(1<<29);
struct edge
{
	int to;
	int dis;
};
struct edge2
{
	int from;
	int to;
	int dis;
};
struct heapnode
{
	int di;
	int num;
	bool operator< (const heapnode j) const
	{
		return di>j.di;
	}
};
edge2 e[100010];
int d[maxn];
int sum[maxn];
int use[maxn];
int n;
vector<edge> G[maxn];
void dijkstra(int s);
int main(void)
{
	int i,u,v,p,q,m,ans;
	edge ed;
	while(scanf("%d%d%d",&n,&m,&q)==3)
	{
		for(i=1;i<=n;i++)
		{
			G[i].clear();
		}
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&u,&v,&p);
			ed.to=v;
			ed.dis=p;
			G[u].push_back(ed);
			e[i].from=u;
			e[i].to=v;
			e[i].dis=p;
		}
		dijkstra(q);
		for(i=1;i<=n;i++)
		{
			sum[i]=d[i];
			G[i].clear();
		}
		for(i=1;i<=m;i++)
		{
			ed.to=e[i].from;
			ed.dis=e[i].dis;
			G[e[i].to].push_back(ed);
		}
		dijkstra(q);
		ans=0;
		for(i=1;i<=n;i++)
		{
			ans=d[i]+sum[i]>ans?d[i]+sum[i]:ans;
		}
		printf("%d\n",ans);
	}
	return 0;
}
void dijkstra(int s)
{
	int i,u,p;
	heapnode h;
	priority_queue<heapnode> heap;
	for(i=1;i<=n;i++)
	{
		d[i]=INF;
		use[i]=0;
	}
	h.di=0;
	h.num=s;
	d[s]=0;
	heap.push(h);
	while(heap.empty()==false)
	{
		h=heap.top();
		heap.pop();
		u=h.num;
		if(use[u]==0)
		{
			use[u]=1;
			p=G[u].size();
			for(i=0;i<p;i++)
			{
				if(d[u]+G[u][i].dis<d[G[u][i].to])
				{
					d[G[u][i].to]=d[u]+G[u][i].dis;
					h.di=d[G[u][i].to];
					h.num=G[u][i].to;
					heap.push(h);
				}
			}
		}
	}
}
时间: 2024-12-13 16:00:15

POJ 3268-Silver Cow Party(dijkstra算法)的相关文章

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 Description One cow from each of \(N\) farms \((1 ≤ N ≤ 1000)\) conveniently numbered \(1..N\) is going to attend the big cow party to be held at farm #\(X (1 ≤ X ≤ N)\). A total of \(M (1 ≤ M ≤ 100,000)\) unidirectional (one-way roads

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 3268 Silver Cow Party(SPFA)

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X

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

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace