1030. Travel Plan (30)

#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

struct node
{
	int next, dis, cost;
};

int main()
{
	int n, m, s, d;
	scanf("%d%d%d%d", &n, &m, &s, &d);

	vector<node> v[510];
	int i, a, b;
	node nod;

	for(i = 1; i <= m; i++)
	{
		scanf("%d%d%d%d", &a, &b, &nod.dis, &nod.cost);

		nod.next = b;
		v[a].push_back(nod);

		nod.next = a;
		v[b].push_back(nod);
	}

	int mark[510], dis[510], cost[510];
	memset(mark, 0, sizeof(mark));
	memset(dis, -1, sizeof(dis));
	memset(cost, -1, sizeof(cost));
	
	int p = s, size, j, next, nextdis, nextcost, min;
	mark[p] = 1;
	dis[p] = cost[p] = 0;

	vector<int> path[510];
	path[p].push_back(p);

	for(i = 1; i <= n - 1; i++)
	{
		size = v[p].size();
		for(j = 0; j <= size - 1; j++)
		{
			next = v[p][j].next;
			if(mark[next] == 1)
			{
				continue;
			}

			nextdis = dis[p] + v[p][j].dis;
			nextcost = cost[p] + v[p][j].cost;

			if(dis[next] == -1 || nextdis < dis[next] || (nextdis == dis[next] 
				&& (cost[next] == -1 || nextcost < cost[next])))
			{
				dis[next] = nextdis;
				cost[next] = nextcost;

				path[next] = path[p];
				path[next].push_back(next);
			}
		}

		min = 10000000;
		for(j = 0; j <= n - 1; j++)
		{
			if(mark[j] == 1 || dis[j] == -1)
			{
				continue;
			}

			if(dis[j] < min)
			{
				min = dis[j];
				p = j;
			}
		}

		if(p == d)
		{
			break;
		}
		mark[p] = 1;
	}

	size = path[d].size();
	for(i = 0; i <= size - 1; i++)
	{
		if(i > 0)
		{
			printf(" ");
		}

		printf("%d", path[d][i]);
	}

	printf(" %d %d\n", dis[d], cost[d]);

	system("pause");
	return 0;
}

 

posted on 2025-11-23 17:01  王景迁  阅读(5)  评论(0)    收藏  举报

导航