POJ 3268


Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9341 Accepted: 4210

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 ≤ XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.



解答:

   很明显,就是用Dijkstra算法,对G和G的补图进行计算。

  第一种办法:代码如下: 该段代码使用邻接矩阵存储。主要是考虑到M可能等于N^2,所以用邻接矩阵可能更省空间。

  

#include<iostream>
#include<cstring>
using namespace std;
//之后可以考虑用临界表做一次

const int N = 1001;
const int MAXTIME = 101*N;//此处是最短路

int Dijkstra(int weight[N][N]  ,int n, int sourcever, int result[N]);
int DijkstraT(int weight[N][N] ,int n, int sourcever, int result[N]);


int main() {
	int weight[N][N];
	memset(weight, 0, sizeof(int) * N * N);
	int n, m, sourcever;
	cin>>n>>m>>sourcever;
	int u, v;
	for(int i = 0; i < m; i++) {
		cin>>u>>v;
		cin>>weight[u][v];
	}
	int total1[N]; memset(total1, 0, (n + 1) *sizeof(int));
	Dijkstra(weight, n, sourcever, total1);
	int total2[N]; memset(total2, 0, (n + 1) *sizeof(int));
	DijkstraT(weight, n, sourcever, total2);
	int max = 0;
	for(int i = 1; i <= n; i++) total2[i] += total1[i];
	for(int i = 1; i <= n; i++) if(total2[i] > max) max = total2[i];
	cout<<max<<endl;
	
}

int Dijkstra(int weight[N][N],int n, int sourcever, int result[N]) {
	bool exist[N];
	for(int i = 1; i <= n; i++) { result[i] = MAXTIME; exist[i] = false; }
	result[sourcever] = 0;
	int count = 0; 
	while(count < n) {
	     int mintime = MAXTIME, mini;
	     for(int i = 1; i <= n; i++) { 
		  if(!exist[i] && result[i] < mintime) {
		  	mintime = result[i];
			mini = i;
		  }
	     }	
	     exist[mini] = true;
	     count++;
	     for(int j = 1; j <= n; j++) 
	     	if(weight[mini][j] != 0 && result[mini] + weight[mini][j] < result[j]) result[j] = result[mini] + weight[mini][j];
	}		
}


int DijkstraT(int weight[N][N],int n, int sourcever, int result[N]) {
	bool exist[N];
	for(int i = 1; i <= n; i++) { result[i] = MAXTIME; exist[i] = false; }
	result[sourcever] = 0;
	int count = 0; 
	while(count < n) {
	     int mintime = MAXTIME, mini;
	     for(int i = 1; i <= n; i++) { 
		  if(!exist[i] && result[i] < mintime) {
		  	mintime = result[i];
			mini = i;
		  }
	     }	
	     exist[mini] = true;
	     count++;
	     for(int j = 1; j <= n; j++) 
	     	if(weight[j][mini] != 0 && result[mini] + weight[j][mini] < result[j]) result[j] = result[mini] + weight[j][mini];
	}		
}



代码的效率: 4632k, 268MS


现在改用邻接表做一次:

#include<iostream>
#include<cstring>
#include<cassert>
using namespace std;
const int N = 1001;
const int MAXTIME = 100 * N;

class GNode {
	public:
		int m_ver;
		int m_weight;
		GNode *m_next;
		GNode(int ver,int weight,  GNode *next):m_ver(ver),m_weight(weight), m_next(next){}
};
class Graph {
	private:
		GNode * m_a[N];
		int m_size;
		int m_DijResult[N];
	public:
		Graph(int n);
		~Graph();
		void m_InsertEdge(int u, int v, int weight);
		int *m_Dijkstra(int source);
		
};

Graph::Graph(int n) {
	m_size = n;
	memset(m_a, 0, sizeof(GNode *) * (n + 1));
}

Graph::~Graph() {
	for(int i = 1; i <= m_size; i++) {
		GNode *iter = m_a[i];
		while(m_a[i] != 0) {
			m_a[i] = iter->m_next;
			delete iter;
			iter = m_a[i];
		}
	}
}

void Graph::m_InsertEdge(int u, int v, int weight) {
	assert(u <= m_size && u > 0);
	assert(v <= m_size && v > 0);
	m_a[u] = new GNode(v, weight, m_a[u]);
}

int* Graph::m_Dijkstra(int source) {
	bool exist[N];	
	for(int i = 1; i <= m_size; i++) {
		exist[i] = false;
		m_DijResult[i] = MAXTIME;
	}

	m_DijResult[source] = 0;

	int count = 0;

	while(count < m_size) {
		int mintime = MAXTIME, mini;
		for(int i = 1; i <= m_size; i++) {
			if(!exist[i] && m_DijResult[i] < mintime) {
				mintime = m_DijResult[i];
				mini = i;
			}
		}

		GNode *iter = m_a[mini];
		count++;
		exist[mini] = true;
		while(iter != 0) {
			if(m_DijResult[iter->m_ver] > m_DijResult[mini] + iter->m_weight) 
				m_DijResult[iter->m_ver] = m_DijResult[mini] + iter->m_weight;
			iter = iter->m_next;
		}

	}
	return m_DijResult;
}

int main() {
	int n,m,source;
	int u, v, weight;
	cin>>n>>m>>source;
	Graph g1(n), g2(n);
	for(int i = 0; i < m; i++) {
		cin>>u>>v>>weight;
		g1.m_InsertEdge(u, v, weight);
		g2.m_InsertEdge(v, u, weight);
	}
	int *a = g1.m_Dijkstra(source);
	int *b = g2.m_Dijkstra(source);

	int result[N];
	for(int i = 1; i <= n; i++)
		result[i] = a[i] + b[i];
	int max = 0;
	for(int i = 1; i <= n; i++) 
		if(result[i] > max) max = result[i];
	cout<<max<<endl;
}

效率:1260K, 344MS

由此可以推之,在OJ上的测试数据没有完全图。

题目很简单,只是运行了两次Dijkstra算法,但是效率上达不到0MS,是个缺憾。 现在先从基础训练起。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值