Group Programming Ladder Race l2-001 emergency rescue

Source: Internet
Author: User
Tags printf time limit

l2-001. Emergency response time 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

——————————————————————————————————

The title means finding the shortest path from S to D and outputting the shortest number of bars and points and the maximum

A few array updates can be opened at Dijkstra, due to the number of bars, update is equal to the case of special processing

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <queue> #include <stack> #include <string> #include <set> #include <
Vector> #include <map> using namespace std;
#define INF 0x3f3f3f3f int edge[505][505];
int vis[505],cnt[505],dis[505],sum[505],pre[505];
int a[505];
	struct node{int id,val;
	BOOL Friend operator< (node A,node b) {return a.val>b.val;

}
};

int n,m;
	void Djstl (int o) {memset (dis,inf,sizeof dis);
	memset (vis,0,sizeof Vis);
	memset (sum,0,sizeof sum);
	memset (cnt,0,sizeof CNT);
	Node f,d;
	F.id=o;
	f.val=0;
	priority_queue<node>q;
	Q.push (f);
	Vis[o]=1;
	dis[o]=0;
	Sum[o]=1;
	Cnt[o]=a[o];
		while (!q.empty ()) {f=q.top ();
		Q.pop ();

		Vis[f.id]=1;
				for (int i=0;i<n;i++) {if (!vis[i]&&f.val+edge[f.id][i]<dis[i]) {dis[i]=f.val+edge[f.id][i];
				Cnt[i]=cnt[f.id]+a[i];
				Pre[i]=f.id;
				Sum[i]=sum[f.id];D.id=i;
				D.val=dis[i];
			Q.push (d); } else if (!vis[i]&&f.val+edge[f.id][i]==dis[i]) {if (Cnt[i]<cnt[f.id]+a[i]) {CNT[I]=CNT[F.I
					D]+a[i];
				Pre[i]=f.id;

			} Sum[i]+=sum[f.id];
		}}}} int main () {int st,ed,u,v,w;
			while (~SCANF ("%d%d%d%d", &n,&m,&st,&ed)) {for (int i=0;i<n;i++) scanf ("%d", &a[i]);
			memset (edge,inf,sizeof edge);
				for (int i=0;i<m;i++) {scanf ("%d%d%d", &u,&v,&w);
				if (W<edge[u][v]) {edge[u][v]=edge[v][u]=w;
			}} memset (pre,-1,sizeof pre);
			Djstl (ST);
			printf ("%d%d\n", sum[ed],cnt[ed]);
			stack<int>s;
			S.push (ed);
            while (pre[ed]!=-1) {s.push (pre[ed]);
            Ed=pre[ed];
            } int q=0;
                while (!s.empty ()) {if (q++) printf ("");
                printf ("%d", s.top ());
            S.pop (); } prinTF ("\ 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.