WOJ-Problem 1009 - The Legend of Valiant Emigration

#include
#include
#include
#define N 105
#define INF 0x7fffffff
#define mcv(a, v) memset(a, v, sizeof(a))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i,n) for (int i=(n);i>=0;i--)
using namespace std;


struct Node{
	int p, s;
	char l[3];
};

int p_l, s_l, dis[N][N],dist[N],pre[N];
int n, m;
Node e[N][N];

inline bool relax(int u, int v){
	if (dist[u] + dis[u][v] < dist[v]){
		dist[v] = dist[u] + dis[u][v];
		return true;
	}
	else
		return false;
}
inline bool judge(Node s){
	if (s.p < p_l&&s.s < s_l)
		return true;
	else
	{
		return false;
	}
}


void bfs(){
	int visit[N];
	mcv(visit, 0);
	queue q;
	q.push(0);
	visit[0] = true;
	dist[0] = 0;
	while (!q.empty()){
		int temp = q.front();
		q.pop();
		rep(i,n)
		if(!visit[i]&&dis[temp][i]&&judge(e[temp][i])&&relax(temp,i))
		{
			q.push(i);
			pre[i] = temp;
			visit[i] = true;
		}
		visit[temp] = false;
	}
}

void sprint(){
	int k = n - 1,top = 0;
	Node st[N];
	while (k){
		st[top++] = e[pre[k]][k];
		k = pre[k];
	}
	repd(i, top-1)
		printf("%s", st[i].l);
}


int main(){
	while (cin >> n >> m){
		rep(i, n) dist[i] = 0x7fffffff;
		mcv(dis, 0); mcv(e, 0); mcv(pre, -1);
		rep(i,m){
			int s, t, pl, sl, v;
			char ch[3];
			cin >> s >> t >> pl >> sl >> v >> ch;
			dis[s][t] = dis[t][s]= v;
			e[s][t].p = e[t][s].p= pl;
			e[s][t].s = e[t][s].s= sl;
			strcpy_s(e[s][t].l, ch);
			strcpy_s(e[t][s].l, ch);
		}
		cin >> p_l >> s_l;
		bfs();
		sprint();
		cout << endl;
	}
}


最短路径,所用的是Bellman-Ford算法。


 
 

你可能感兴趣的:(算法)