l2-001. Emergency rescue (Dijkstra algorithm)

Source: Internet
Author: User
Tags stdin
l2-001. Emergency rescueTime limit of MS
Memory Limit 65536 KB
Code length limit 8000 B
Chen, standard author of the procedure for the award of questions

As the head of a city's emergency rescue team, you have a special national map. The map shows a number of scattered cities and some quick roads connecting the city. The number of rescue teams in each city and the length of each fast road connecting two cities are marked on the map. When other cities have an emergency call for you, your task is to lead your rescue team to the scene as quickly as possible, while gathering as many rescue teams along the way.

Input Format:

Enter the first line to give 4 positive integers n, M, S, D, where N (2<=n<=500) is the number of cities, by the way assuming the city number is 0~ (N-1), M is the number of fast roads, S is the city number of the origin, and D is the city number of the destination. The second line gives n positive integers, where number i is the number of rescue teams in City I, separated by a space between the numbers. In the subsequent M-line, each line gives a quick path of information, namely: City 1, City 2, the length of the fast road, the middle with a space separated, the numbers are integers and not more than 500. The input guarantees that rescue is feasible and the optimal solution is unique.

output Format:

The first line outputs the number of different shortest paths and the maximum number of rescue teams that can be convened. The second line outputs the city number that passes through the path from S to D. The numbers are separated by a space, and the output cannot have extra spaces. Input Sample:

4 5 0 3 (+) 0 1 1 1 3 2 0
3 3
0 2 2
2 3 2
Sample output:
2
0 1 3
First, borrowed the Dijkstra framework #include <iostream> #include <algorithm> #include <vector> #include <cstring&
Gt

#define INF 1 << using namespace std; int weight[500] = {0};
Number of rescue teams in each city int totweight[500] = {0};
int pathamount[500] = {0};
int g[510][510];
int path[510];
int dis[510];
int vis[510];
int N, M, S, D;

int num;
	void Dijkstra (int s) {for (int i = 0; i < N; i++) dis[i] = INF;
	Vis[s] = true;

	Dis[s] = 0;
			for (int i = 0; i < N; i++) {if (G[s][i]! = INF && I! = s) {Dis[i] = G[s][i];
			Path[i] = s;
			Totweight[i] = Weight[s] + weight[i];
		Pathamount[i] = 1;		
		}} for (int i = 0; i < N-1; i++) {int min = INF, u =-1;
				for (int j = 0; J < N; J + +) {if (!vis[j] && dis[j] < min) {min = dis[j];
			U = j;
		}} Vis[u] = true;  for (int j = 0; J < N; J + +) {if (!vis[j]) {if (Dis[u] + g[u][j] < Dis[j]) {dis[j] = Dis[u] +
					G[U][J];
	PATH[J] = u;				TOTWEIGHT[J] = Totweight[u] + weight[j];
				PATHAMOUNT[J] = Pathamount[u];
					} else if (Dis[u] + g[u][j] = = Dis[j]) {Pathamount[j] + = Pathamount[u];
						if (Totweight[j] < Totweight[u] + Weight[j]) {totweight[j] = Totweight[u] + weight[j];
					PATH[J] = u;
	}}}}}} int main () {//freopen ("Data.txt", "R", stdin);
	int re[510];
	CIN >> N >> M >> S >> D;
		for (int i = 0; i < N; i++) {cin >> num;
	Weight[i] = num;
	} for (int i = 0, i < n; i++) for (int j = 0; J < N; J + +) g[i][j] = INF;
		for (int i = 0; i < M; i++) {int start, end, length;
		CIN >> Start >> end >> length;
			if (start! = end) {G[start][end] = min (g[start][end], length);
		G[end][start] = G[start][end];
	}} Dijkstra (S);
	int num = 0, cur = D;
		while (cur! = S) {re[num++] = cur;
	cur = path[cur];
	} re[num++] = S; cout << Pathamount[d] << "" << Totweight[d] << Endl;
	for (int i = num-1; i > 0; i--) cout << re[i] << "";
	cout << re[0];
return 0; }

The second, using DFS, is easier to understand than the first, but unfortunately time out #include <iostream> #include <vector> #include <algorithm> #include <
Cstring> using namespace std;

struct Node {int end, length;}; For convenience, define global variables int minlen = 1 << 30; The shortest path length int minrodeamount = 1; Number of shortest paths int maxpeopleamount =-1; Maximum number of rescue teams int totallen = 0; Currently traversed by the length int totalpeople = 0;//The number of rescue teams currently called int visited[500] = {0}; City Access Mark int weight[500] = {0}; Number of rescue teams in each city int path[500]; Access path int min_path[500];
Shortest path int minl[500][250000] = {0};
int min_depth = 0; int depth = 0; Access depth vector< vector <node> > G (510);
The path of the city int N, M, S, D;
int num;

int start, end;
    void Dfs (int v) {if (v = = D) {path[depth++] = V;
    if (Minlen < Totallen) return;
      else if (Minlen > Totallen) {minrodeamount = 1;
      Minlen = Totallen;
      memcpy (Min_path, path, depth * sizeof (int));
      min_depth = depth;
    Maxpeopleamount = Totalpeople; } else if (Minlen= = Totallen) {minrodeamount++;
        if (Maxpeopleamount < totalpeople) {memcpy (Min_path, path, depth * sizeof (int));
        min_depth = depth;
      Maxpeopleamount = Totalpeople;
  }} return;
  } path[depth++] = V; for (int i = 0; i < g[v].size (); i++) {if (!visited[g[v][i].end]) {if (Totallen + g[v][i].length > Minl
      EN) continue;  if (Totalpeople + weight[g[v][i].end] <= Minl[g[v][i].end][totallen + g[v][i].length]) {minrodeamount++;
      Continue
      } else Minl[g[v][i].end][totallen + g[v][i].length] = totalpeople + weight[g[v][i].end];
      Totallen + = G[v][i].length;
      Totalpeople + = Weight[g[v][i].end];
      VISITED[V] = 1;
      Dfs (G[v][i].end);
      Totallen-= g[v][i].length;
      Totalpeople-= Weight[g[v][i].end];
      VISITED[V] = 0;
    depth--;
}} return;
  } int main () {//freopen ("Data.txt", "R", stdin); CIN >> N >> M >> S >>D
    for (int i = 0; i < N; i++) {cin >> num;
  Weight[i] = num;
    } for (int i = 0; i < M; i++) {node n;
    int end, length;
    CIN >> Start >> end >> length;
      if (start! = end) {n.end = end; n.length = length;
      G[start].push_back (n); N.end = start;
      n.length = length;
    G[end].push_back (n);
  }} Totalpeople + = Weight[s];
  Dfs (S);
    if (min_depth > 0) {cout << minrodeamount << "" << maxpeopleamount << Endl;
      for (int i = 0; i < min_depth; i++) {if (!i) cout << min_path[i];
    else cout << "<< min_path[i";
}} 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.