poj1797 Heavy Transportation SPFA Deformation __POJ

Source: Internet
Author: User
Tags cas
The following:

Given a non-direction graph, the maximum value of the minimum value from the path of 1 to n is obtained.

That is, there may be more than one path from 1 to n, and each path has an edge with the smallest weight, asking the maximum value of those edges.


idea:

The previously SPFA dist Array records the distance, and now only changes to the minimum value from the source point to I.

The original SPFA extension node:

for (int i = 0; i < edges[cur].size (); i++) {
	Edge e = edges[cur][i];
	if (<strong>dist[cur] + e.v < dist[e.to]</strong>) {
		dist[e.to] = dist[cur] + e.v
		if (!vis[e.to)) {
			vis[e.to] = 1;
			Q.push (e.to);}}

Now just change it to:

for (int i = 0; i < edges[cur].size (); i++) {
	Edge e = edges[cur][i];
	if (<strong>min (Dist[cur], e.v) > dist[e.to]</strong>) {
		dist[e.to] = min (dist[cur), e.v);
		if (!vis[e.to]) {
			vis[e.to] = 1;
			Q.push (e.to);}}
Can.


Code (1836K,282MS):

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <

Vector> #include <queue> using namespace std;
	struct edge{int to, V;

Edge (int A, int b): to (a), V (b) {}};
int T;
int n, m; Vector<edge> edges[1005];
adjacency table int vis[1005];

int dist[1005];
	int SPFA () {queue<int> q;
	memset (Vis, 0, sizeof (VIS));
	memset (Dist, 0, sizeof (Dist));
	Q.push (1);
	VIS[1] = 1;
	DIST[1] = 0x3f3f3f3f;
		while (!q.empty ()) {int cur = q.front ();
		Q.pop ();
			for (int i = 0; i < edges[cur].size (); i++) {Edge e = edges[cur][i];
				if (min (dist[cur], e.v) > Dist[e.to]) {dist[e.to] = min (dist[cur), e.v);
					if (!vis[e.to]) {vis[e.to] = 1;
				Q.push (e.to);
	}} Vis[cur] = 0;
return dist[n];
	int main () {scanf ("%d", &t);
	int cas = 1;
		while (t--) {scanf ("%d%d", &n, &m);
		for (int i = 0; I <= N; i++) edges[i].clear ();
		int A, b, C; for (int i = 0; i < m; I+ +) {scanf ("%d%d%d", &a, &b, &c);
			Edges[a].push_back (Edge (b, c));
		Edges[b].push_back (Edge (A, c));
		int ans = SPFA ();
	printf ("Scenario #%d:\n%d\n\n", cas++, ans);
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.