c_cpp 旅游指南

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 旅游指南相关的知识,希望对你有一定的参考价值。

#include <vector>
#include <queue>
#include <iostream>
using namespace std;

typedef int vertex;
typedef vector< pair<vertex,int> > edge_list;

const int MAX=1<<30;

class graph {
	vector<edge_list> edge;
	
public:
	graph(int size) : edge(1+size) {}
	
	void add_edge(vertex u,vertex v,int w) {
		edge[u].push_back(make_pair(v,w));
		edge[v].push_back(make_pair(u,w));
	}
	
	const edge_list &get_edge(vertex u) const {
		return edge[u];
	}
};

struct candidate_vertex {
	vertex key;
	int value;
	
	candidate_vertex(vertex v,int bound) : key(v),value(bound) {}
	bool operator <(const candidate_vertex &obj) const {
		return value<obj.value;
	}
};

int bound_dijkstra(int n,const graph &g,int s,int d) {
	vector<bool> pass(1+n);
	vector<int> bound(1+n);
	priority_queue<candidate_vertex> heap;
	
	heap.push(candidate_vertex(s,bound[s]=MAX));
	while (heap.size()>0) {
		vertex u=heap.top().key;
		if (u==d) break;
		heap.pop();
		if (pass[u]) continue;
		pass[u]=true;
		
		const edge_list &list=g.get_edge(u);
		for (int i=0;i<list.size();i++) {
			vertex v=list[i].first;
			if (pass[v]) continue;
			
			int alt=min(bound[u],list[i].second);
			bound[v]=max(bound[v],alt);
			heap.push(candidate_vertex(v,bound[v]));
		}
	}
	return bound[d];
}

int minimum_trips(int n,const graph &g,int s,int d,int t) {
	int capacity=bound_dijkstra(n,g,s,d)-1;
	return t/capacity + (t%capacity>0? 1: 0);
}

int main() {
	int k=1,n,r;
	while (cin>>n>>r) {
		if (n==0&&r==0) break;
		
		graph g(n);
		for (int i=0;i<r;i++) {
			int c1,c2,p;
			cin>>c1>>c2>>p;
			g.add_edge(c1,c2,p);
		}
		int s,d,t;
		cin>>s>>d>>t;
		
		cout<<"Scenario #"<<k++<<endl;
		cout<<"Minimum Number of Trips = "<<minimum_trips(n,g,s,d,t)<<endl;
		cout<<endl;
	}
	return 0;
}

以上是关于c_cpp 旅游指南的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用C ++ 17模板推导指南作为元函数。请参阅http://melpon.org/wandbox/permlink/lVFBj7shPP0fUMuQ

c_cpp 130.周围地区

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET