麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

PAT甲級1030

2019-11-11 04:49:12
字體:
供稿:網(wǎng)友

1030. Travel Plan (30)

時間限制400 ms內(nèi)存限制65536 kB代碼長度限制16000 B判題程序Standard作者CHEN, Yue

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a PRogram to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input
4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20Sample Output
0 2 3 3 40
#include<cstdio>#include<vector>#include<queue>#include<algorithm>using namespace std;const int maxn = 510;const int INF = 1000000000;struct Node{	int v, dis, cost;}node;struct compare{	bool Operator()(Node n1, Node n2)	{		return n1.dis > n2.dis;	}};vector<Node> Adj[maxn];int costs[maxn][maxn];bool vis[maxn] = { false };int d[maxn];//int costs[maxn];vector<int> pre[maxn];int N, M, S, D;void Dijkstra(int s){	fill(d, d + maxn, INF);//	fill(costs, costs + maxn, 0);	d[s] = 0;//	costs[s] = 0;	priority_queue<Node, vector<Node>, compare> Q;	node.v = s; node.dis = d[s]; //node.cost = costs[s];	Q.push(node);	int u;	for (int i = 0; i < N; i++)	{		if (!Q.empty())		{			u = Q.top().v;			vis[u] = true;			Q.pop();		}		else			return;		for (int i = 0; i < Adj[u].size(); i++)		{			int v = Adj[u][i].v;			int dis = Adj[u][i].dis;		//	int cost = Adj[u][i].cost;			if (!vis[v])			{				int t = d[u] + dis;				if (t < d[v])				{					d[v] = t;					pre[v].clear();					pre[v].push_back(u);					node.v = v; node.dis = t; //node.cost = cost;					Q.push(node);				}				else if (t == d[v])				{					pre[v].push_back(u);				}			}		}	}}vector<int> path, tempPath;int minCost = INF;void DFS(int v){	if (v == S)	{		tempPath.push_back(v);		int cost = 0;		int i = tempPath.size();		for (i--; i >0; i--)		{			int id = tempPath[i], idnext = tempPath[i - 1];			cost += costs[id][idnext];		}		if (cost < minCost)		{			minCost = cost;			path = tempPath;		}		tempPath.pop_back();		return;	}	tempPath.push_back(v);	for (int i = 0; i < pre[v].size(); i++)	{		DFS(pre[v][i]);	}	tempPath.pop_back();}void PrintPath(){	for (int i = path.size() - 1; i >= 0; i--)	{		printf("%d ", path[i]);	}}int main(){	scanf("%d%d%d%d", &N, &M, &S, &D);	int City1, City2, Distance, Cost;	for (int i = 0; i < M; i++)//這里輸入的是邊	{		scanf("%d%d%d%d", &City1, &City2, &Distance, &Cost);		node.v = City2; node.dis = Distance; 		costs[City1][City2] = Cost;		costs[City2][City1] = Cost;//node.cost = Cost;		Adj[City1].push_back(node);		node.v = City1;		Adj[City2].push_back(node);	}	Dijkstra(S);	DFS(D);	PrintPath();	printf("%d %d/n", d[D], minCost);	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美视频国产 | 毛片免费看电影 | 免费毛片视频播放 | 姑娘第四集免费看视频 | 久久久久久久久久久综合 | 亚洲福利视 | 4p一女两男做爰在线观看 | 亚洲一区二区三区四区精品 | videos高潮| av免费提供| 国产精品视频一区二区噜噜 | 国产激情精品一区二区三区 | 欧美成人综合视频 | 色婷婷久久久久久 | 免费久久精品 | 久久久久电影网站 | 黄色免费在线视频网站 | 全免费午夜一级毛片真人 | 欧美视频99 | 欧美日韩一区,二区,三区,久久精品 | 国产成人av免费看 | 黄色毛片a级 | 欧美亚洲一区二区三区四区 | 日本aaaa片毛片免费观蜜桃 | 午夜视 | 成人毛片免费播放 | 国产午夜免费视频 | 日韩字幕| 精品一区二区在线播放 | 国产精品爱久久久久久久 | 看片一区| 天天看成人免费毛片视频 | 一区二区久久精品66国产精品 | 国产九九热 | 国产乱淫a∨片免费观看 | 在线观看精品视频 | freexxx69性欧美hd | 日日爱99| 7777在线观看 | 日本免费aaa观看 | 国产精品久久久久久久久久电影 |