UVA 11280-flying to Fredericton (shortest way)

Source: Internet
Author: User

Topic Link: UVA 11280-flying to Fredericton


Add one-dimensional means to arrive at the point of docking several times, then Dijsktra do the shortest way.


#include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #
Include <queue> #include <iostream> #include <algorithm> using namespace std;
typedef pair<int,int> PII;
const int MAXN = 105;

const int inf = 0X3F3F3F3F;
BOOL DONE[MAXN][MAXN];
int N, M, D[MAXN][MAXN];
Vector<pii> G[MAXN];

Map<string, int> T;
	void init () {int D;
	Char CITY1[MAXN], CITY2[MAXN];

	T.clear ();
	scanf ("%d", &n);
		for (int i = 0; i < N; i++) {g[i].clear ();
		scanf ("%s", city1);
	T[city1] = i;
	} scanf ("%d", &m);
		for (int i = 0; i < M; i++) {scanf ("%s%s%d", City1, City2, &d);
		int u = t[city1], v = t[city2];
	G[u].push_back (Make_pair (V, D));
	}}/* bool SPFA (int s, int e) {for (int i = 0; I <= N; i++) for (int j = 0; J <= 101; j + +) d[i][j] = inf;
	memset (inque, 0, sizeof (inque));
	memset (otque, 0, sizeof (otque));

	D[s][0] = 0;
	Queue<pii> Q;
Q.push (Make_pair (s, 0));	while (!
		Q.empty ()) {int u = Q.front (). First;
		int t = Q.front (). Second;

		Q.pop ();
		Inque[u][t] = 0;
		otque[u][t]++;
		if (Otque[u][t] > N) return false;
			for (int i = 0; i < g[u].size (); i++) {int v = g[u][i].first, w = g[u][i].second;

			if (d[v][t+1] > D[u][t] + w) d[v][t+1] = d[u][t] + W;
				if (t <= &&!inque[v][t+1]) {inque[v][t+1] = 1;
			Q.push (Make_pair (V, T + 1));
}}} return true;
	} */struct state {int u, T, D;  state (int u = 0, int t = 0, int d = 0): U (U), T (t), D (d) {} BOOL operator < (const state& a) const {return d > A.D;

}
};
	void Dijkstra (int s) {for (int i = 0; i < N; i++) for (int j = 0; J <=; j + +) d[i][j] = inf;
	D[s][0] = 0;

	memset (done, 0, sizeof (done));
	Priority_queue<state> Q;

	Q.push (state (S, 0, d[s][0])); while (!
		Q.empty ()) {int u = q.top (). u;
		int t = q.top (). T;

		Q.pop ();
		if (done[u][t]) continue;

		Done[u][t] = true; for (int i = 0; i < g[u].size ();
			i++) {int v = g[u][i].first, w = g[u][i].second;
				if (d[v][t+1] > D[u][t] + W) {d[v][t+1] = D[u][t] + W;
			Q.push (State (V, t+1, d[v][t+1]));
	}}}} int main () {int cas;
	scanf ("%d", &cas);
		for (int kcas = 1; kcas <= cas; kcas++) {if (Kcas! = 1) printf ("\ n");
		Init ();

		Dijkstra (0);
		printf ("Scenario #%d\n", Kcas);
		int q, x;
		scanf ("%d", &q);
			while (q--) {scanf ("%d", &x);
			int ans = inf;
			x = min (x+1, N-1);
			for (int i = 0; I <= x; i++) ans = min (ans, d[n-1][i]);
			if (ans! = inf) printf ("Total cost of flight (s) is $%d\n", ans);
		else printf ("No satisfactory flights\n");
}} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.