1111 Online Map

题目

题意:给一张地图,两个结点中既有距离也有时间,有的单行有的双向,要求根据地图推荐两条路线:一条是最快到达路线,一条是最短距离的路线。
第一行给出两个整数N和M,表示地图中地点的个数和路径的条数。接下来的M行每一行给出:道路结点编号V1 道路结点编号V2 是否单行线 道路长度 所需时间
要求第一行输出最快到达时间Time和路径,第二行输出最短距离Distance和路径

tip:DFS + Dijkstra

DFS版 

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int mapd[503][503];
int mapt[503][503];
vector<int>map[503];
vector<int>dist,tim,temp;
int n,m,s,e,mincost=1e7;
int mincount=1e7,minlen=1e7,minspend=1e7,checked[503]= {0};
void dfsdistance(int t,int len,int spend) {
	checked[t]=1;
	temp.push_back(t);
	if(len>minlen)
		return ;
	if(t==e) {
		mincost=min(mincost,spend);
		if(minlen==len) {
			if(minspend>spend) {
				minspend=spend;
				dist=temp;
			}
		} else if(minlen>len) {
			minlen=len;
			minspend=spend;
			dist=temp;
		}
		return ;
	}
	for(int i=0; i<map[t].size(); ++i)
		if(!checked[map[t][i]]) {
			dfsdistance(map[t][i],len+mapd[t][map[t][i]],mapt[t][map[t][i]]+spend);
			checked[map[t][i]]=0;
			temp.pop_back();
		}
}
void dfstime(int t,int spend,int count) {
	checked[t]=1;
	temp.push_back(t);
	if(spend>mincost)
		return ;
	if(t==e) {
		if(mincost==spend) {
			if(mincount>count) {
				mincount=count;
				tim=temp;
			}
		} else if(mincost>spend) {
			mincount=count;
			mincost=spend;
			tim=temp;
		}
		return ;
	}
	for(int i=0; i<map[t].size(); ++i)
		if(!checked[map[t][i]]) {
			dfstime(map[t][i],spend+mapt[t][map[t][i]],count+1);
			checked[map[t][i]]=0;
			temp.pop_back();
		}
}
int main() {
	scanf("%d %d",&n,&m);
	memset(mapd,0,sizeof(mapd));
	memset(mapt,0,sizeof(mapt));
	for(int i=0; i<m; ++i) {
		int a,b,way,len,time;
		scanf("%d %d %d %d %d",&a,&b,&way,&len,&time);
		mapd[a][b]=len;
		mapt[a][b]=time;
		map[a].push_back(b);
		if(!way) {
			mapd[b][a]=len;
			mapt[b][a]=time;
			map[b].push_back(a);
		}
	}
	scanf("%d %d",&s,&e);
	dfsdistance(s,0,0);
	temp.clear();
	memset(checked,0,sizeof(checked));
	dfstime(s,0,1);
	if(dist==tim) {
		printf("Distance = %d; Time = %d: %d",minlen,mincost,tim[0]);
		for(int i=1; i<tim.size(); ++i)
			printf(" -> %d",tim[i]);
	} else {
		printf("Distance = %d: %d",minlen,dist[0]);
		for(int i=1; i<dist.size(); ++i)
			printf(" -> %d",dist[i]);
		printf("\nTime = %d: %d",mincost,tim[0]);
		for(int i=1; i<tim.size(); ++i)
			printf(" -> %d",tim[i]);
	}
	return 0;
}

 Dijkstra版

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 999999999;
int dis[510], Time[510], e[510][510], w[510][510], dispre[510],Timepre[510], weight[510],NodeNum[510];
bool visit[510];
vector<int> dispath, Timepath, temppath;
int st, fin, minnode = inf;
void dfsdispath(int v) {
	dispath.push_back(v);
	if(v == st) return ;
	dfsdispath(dispre[v]);
}
void dfsTimepath(int v) {
	Timepath.push_back(v);
	if(v == st) return ;
	dfsTimepath(Timepre[v]);
}
int main() {
	fill(dis, dis + 510, inf);
	fill(Time, Time + 510, inf);
	fill(weight, weight + 510, inf);
	fill(e[0], e[0] + 510 * 510, inf);
	fill(w[0], w[0] + 510 * 510, inf);
	int n, m;
	scanf("%d %d", &n, &m);
	int a, b, flag, len, t;
	for(int i = 0; i < m; i++) {
		scanf("%d %d %d %d %d", &a, &b, &flag, &len, &t);
		e[a][b] = len;
		w[a][b] = t;
		if(flag != 1) {
			e[b][a] = len;
			w[b][a] = t;
		}
	}
	scanf("%d %d", &st, &fin);
	dis[st] = 0;
	for(int i = 0; i < n; i++)
		dispre[i] = i;
	for(int i = 0; i < n; i++) {
		int u = -1, minn = inf;
		for(int j = 0; j < n; j++) {
			if(visit[j] == false && dis[j] < minn) {
				u = j;
				minn = dis[j];
			}
		}
		if(u == -1) break;
		visit[u] = true;
		for(int v = 0; v < n; v++) {
			if(visit[v] == false && e[u][v] != inf) {
				if(e[u][v] + dis[u] < dis[v]) {
					dis[v] = e[u][v] + dis[u];
					dispre[v] = u;
					weight[v] = weight[u] + w[u][v];
				} else if(e[u][v] + dis[u] == dis[v] && weight[v] > weight[u] + w[u][v]) {
					weight[v] = weight[u] + w[u][v];
					dispre[v] = u;
				}
			}
		}
	}
	dfsdispath(fin);
	Time[st] = 0;
	fill(visit, visit + 510, false);
	for(int i = 0; i < n; i++) {
		int u = -1, minn = inf;
		for(int j = 0; j < n; j++) {
			if(visit[j] == false && minn > Time[j]) {
				u = j;
				minn = Time[j];
			}
		}
		if(u == -1) break;
		visit[u] = true;
		for(int v = 0; v < n; v++) {
			if(visit[v] == false && w[u][v] != inf) {
				if(w[u][v] + Time[u] < Time[v]) {
					Time[v] = w[u][v] + Time[u];
					Timepre[v]= u;
					NodeNum[v]=NodeNum[u]+1;
				} else if(w[u][v] + Time[u] == Time[v]&&NodeNum[u]+1<NodeNum[v]) {
					Timepre[v]= u;
					NodeNum[v]=NodeNum[u]+1;
				}
			}
		}
	}
	dfsTimepath(fin);
	printf("Distance = %d", dis[fin]);
	if(dispath == Timepath) {
		printf("; Time = %d: ", Time[fin]);
	} else {
		printf(": ");
		for(int i = dispath.size() - 1; i >= 0; i--) {
			printf("%d", dispath[i]);
			if(i != 0) printf(" -> ");
		}
		printf("\nTime = %d: ", Time[fin]);
	}
	for(int i = Timepath.size() - 1; i >= 0; i--) {
		printf("%d", Timepath[i]);
		if(i != 0) printf(" -> ");
	}
	return 0;
}

 

1111 Online Map1111 Online Map 江楚郎(已婚 发布了342 篇原创文章 · 获赞 15 · 访问量 1万+ 私信 关注
上一篇:C#WPF数据绑定模板化操作四步走


下一篇:java基础- 你真的了解运算符吗?