Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

spfa 飘过~~

Posted by a7884594 at 2015-03-04 16:51:32 on Problem 2263
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <map>

using namespace std;

const int inf = 100000000;
const int maxn = 205;

struct node{
	int v;
	int w;
	
	node(int _v, int _w){
		v = _v; w = _w;
	}
};

int n,m;
map<string,int> Map;
vector<node> list[maxn];
int s,t;//起点、终点 
int dist[maxn];
bool inq[maxn];

bool input(){
	string u,v;
	int w;
	int cnt = 1;
	cin>>n>>m;
	if(n+m == 0) return false;
	//清零
	for(int i = 0; i <= n; i++) list[i].clear();
	
	Map.clear(); 
	 for(int i = 0; i < m; i++){
	 	cin>>u>>v;
	 	cin>>w;
	 	if(Map[u] == 0) Map[u] = cnt++;
	 	if(Map[v] == 0) Map[v] = cnt++;
	 	list[Map[u]].push_back(node(Map[v],w));
	 	list[Map[v]].push_back(node(Map[u],w));
	 }
	 string a,b;
	 cin>>a>>b;
	 s = Map[a];
	 t = Map[b];	 
	 return true;
}


void spfa(){
	queue<int> q;
	for(int i = 0; i <= n; i++){
		dist[i] = -inf;
		inq[i] = false;
	}
	dist[s] = inf;
	q.push(s);
	inq[s] = true;
	while(!q.empty()){
		int u = q.front(); q.pop(); inq[u] = false;
		
		for(int i = 0; i < list[u].size(); i++){
			int v = list[u][i].v;
			int w = min(dist[u],list[u][i].w);//取上个城市和扩展边之间的最小值 
			if(w > dist[v]){//发现载重更大的路径 
				dist[v] = w;
				if(!inq[v]){
					q.push(v);
					inq[v] = true;
				}

			}
		}
	}	
}

void solve(){
	static int cnt = 0;
	spfa();
	printf("Scenario #%d\n",++cnt);
	printf("%d tons\n",dist[t]);
	puts("");
}

int main(){ 
	
	while(input()){
		solve();
	}
	
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator