PAT——最短路径

 1018 Public Bike Management (30 分)  (Dijkstra+dfs)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex SSS is the current number of bikes stored at SSS. Given that the maximum capacity of each station is 10. To solve the problem at S3S_3S​3​​, we have 2 different shortest paths:

  1. PBMC -> S1S_1S​1​​ -> S3S_3S​3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1S_1S​1​​ and then take 5 bikes to S3S_3S​3​​, so that both stations will be in perfect conditions.

  2. PBMC -> S2S_2S​2​​ -> S3S_3S​3​​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: CmaxC_{max}C​max​​ (≤100\le 100≤100), always an even number, is the maximum capacity of each station; NNN (≤500\le 500≤500), the total number of stations; SpS_pS​p​​, the index of the problem station (the stations are numbered from 1 to NNN, and PBMC is represented by the vertex 0); and MMM, the number of roads. The second line contains NNN non-negative numbers CiC_iC​i​​ (i=1,⋯,Ni=1,\cdots ,Ni=1,⋯,N) where each CiC_iC​i​​ is the current number of bikes at SiS_iS​i​​ respectively. Then MMM lines follow, each contains 3 numbers: SiS_iS​i​​, SjS_jS​j​​, and TijT_{ij}T​ij​​ which describe the time TijT_{ij}T​ij​​ taken to move betwen stations SiS_iS​i​​ and SjS_jS​j​​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0−>S1−>⋯−>Sp0->S_1->\cdots ->S_p0−>S​1​​−>⋯−>S​p​​. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of SpS_pS​p​​ is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

题意:

  • cmax——自行车站最大容量
  • n个自行车站点(1~n),中心顶点为0
  • m条道路,在该路上花费时间t
  • sp——问题车站的顶点序号
  • 自行车站三种状态:prefect (num=cmax/2)   need需总部往外派车 (num<cmax/2)   back需总部往回拉车 (num>cmax/2)
  • 求一出行路线,使sp变为perfect状态,路径最短(即所需时间最少),所需need最少(need相同时back最少)

思路:

最短路径:Dijkstra

  • dis[ ]
  • mp[ ][ ]
  • vis[ ]
  • vector<int>pre[505](记录节点的前一个节点)以助dfs

need最少:dfs

  • w[ ] 记录个顶点与perfect状态之差
  • vector<int> tmpPath(当前尝试的路径)
  • vector<int> path  (最终答案,从sp到0逆序)
  1. 递归出口,回溯到0,而后开始计算need和back,遍历tmpPath中每个节点的w[i], 三种情况(w[i]>0,w[i]<0&&back够,w[i]<0&&back不够need来凑),更新minNeed、minBack、path或minBack、path。之后经行下一个尝试,删掉当前元素tmpPath.pop_back()并return
  2. 函数体内部,遍历pre[v].size() 依次深搜
  3. tmpPath.pop_back()
  • fill(mp[0],mp[0]+505*505,inf);
  • fill(dis,dis+505,inf);
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int w[505],dis[505],mp[505][505],vis[505];
vector<int> pre[505],tmpPath,path;
const int inf=999999999;
int minBack=inf,minNeed=inf;
int cmax,n,sp,m;
void dfs(int v) {
	tmpPath.push_back(v);
	if(v==0) {
		int need=0,back=0;
		for(int i=tmpPath.size()-1;i>=0;i--) {             //遍历tmpPath中个点的w[i]
			int id=tmpPath[i];
			if(w[id]>0) back+=w[id];
			else {
				if(back>-w[id]) back+=w[id];
				else need+=-w[id]-back,back=0;
			}
		}
		if(minNeed>need) {                                //题目需求,先路径最短,后need最少,后back最少
			minNeed=need;
			minBack=back;
			path=tmpPath;
		}
		else if(minNeed==need&&minBack>back){
				minBack=back;
				path=tmpPath;
		}
		tmpPath.pop_back();                              //进行下一尝试,退出当前元素
		return;
	}
	for(int i=0;i<pre[v].size();i++)
		dfs(pre[v][i]);
	tmpPath.pop_back();                                      //再回退一步,走一下最后两步试试
} 
int main() {
	fill(mp[0],mp[0]+505*505,inf);
	fill(dis,dis+505,inf);
	cin>>cmax>>n>>sp>>m;
	for(int i=1; i<=n; i++) {
		cin>>w[i];
		w[i]-=cmax/2;
	}
	int a,b,l;
	for(int i=0; i<m; i++) {
		cin>>a>>b>>l;
		mp[a][b]=mp[b][a]=l;
	}
	dis[0]=0;
	for(int i=0; i<=n; i++) {
		int minn=inf,u=-1;
		for(int j=0; j<=n; j++)
			if(vis[j]==0&&minn>dis[j]) minn=dis[j],u=j;
		if(u==-1) break;
		vis[u]=1;
		for(int j=0; j<=n; j++) {
			if(vis[j]==0&&mp[u][j]!=inf) {
				if(dis[j]>dis[u]+mp[u][j]) {
					dis[j]=dis[u]+mp[u][j];
					pre[j].clear();            //之前所有须全部清除
					pre[j].push_back(u);
				} else if(dis[j]==dis[u]+mp[u][j])
					pre[j].push_back(u);
			}
		}
	}
	dfs(sp);
	printf("%d 0",minNeed);
	for(int i=path.size()-2;i>=0;i--) printf("->%d",path[i]);
	printf(" %d",minBack);
	return 0;
}

1030 Travel Plan (30 分) (Dijkstra+dfs)

  • n个点,m条路,起点s,终点d
  • m个输入:a,b,len,cost
  • 打印最短路径,路径相同时打印最少花费路径
  1. Dijkstra:求最短路径,并计入pre
  2. dfs:深搜pre,到达目标计算sumcos更新path
  3. 注意:一次push_back(),两次pop_back();
#include<iostream>
#include<vector>
using namespace std;
const int inf=99999999;
int n,m,s,d;
int minc=inf;
int dis[505],mpd[505][505],mpc[505][505],vis[505];
vector<int> pre[505],tmpPath,path;
void dfs(int v){
	tmpPath.push_back(v);
	if(v==s){
		int sumc=0,a,b;
		for(int i=0;i<tmpPath.size()-1;i++){
			a=tmpPath[i];
			b=tmpPath[i+1];
			sumc+=mpc[a][b];
		}
		if(sumc<minc){
			minc=sumc;
			path=tmpPath;
		}
		tmpPath.pop_back();
		return;
	}
	for(int i=0;i<pre[v].size();i++)
		dfs(pre[v][i]);
	tmpPath.pop_back();
}
int main() {
	fill(mpd[0],mpd[0]+505*505,inf);
	fill(mpc[0],mpc[0]+505*505,inf);
	fill(dis,dis+505,inf);
	cin>>n>>m>>s>>d;
	int a,b,l,c;
	for(int i=0;i<m;i++){
		cin>>a>>b>>l>>c;
		mpd[a][b]=mpd[b][a]=l;
		mpc[a][b]=mpc[b][a]=c;	
	}
	dis[s]=0;
	for(int i=0;i<n;i++){
		int minn=inf,u=-1;
		for(int j=0;j<n;j++)
			if(vis[j]==0&&dis[j]<minn) minn=dis[j],u=j;
		if(u==-1)break;
		vis[u]=1;
		for(int j=0;j<n;j++){
			if(vis[j]==0&&mpd[u][j]!=inf){
				if(dis[j]>dis[u]+mpd[u][j]){
					dis[j]=dis[u]+mpd[u][j];
					pre[j].clear();
					pre[j].push_back(u);
				}else if(dis[j]==dis[u]+mpd[u][j])
					pre[j].push_back(u);
			}
		}
	}
	dfs(d);
	cout<<path[path.size()-1];
	for(int i=path.size()-2;i>=0;i--) cout<<' '<<path[i];
	printf(" %d %d",dis[d],minc);
	return 0;
}

1087 All Roads Lead to Rome (30 分)

题意:

  • n个点(每个点都是三个大写字母组成的字符),k条路,起点start
  • 除起点外每个点都有happiness值
  • 已知k条路每条路上花费的cost值
  • 花钱最少的路径,花钱同找happiness值最大的路径,happiness值同找avgh最大的路径
  • 输出最短路径数目,最小花费,maxh,avgh

点的名称处理:

  • 输入——字符串变数字序号。将点名字编号,起点0,其余点在读入hap值时赋值为1到n-1
  • 两个map保存字符串和序号相互的对应关系,处理图时用序号
  • 输出——数字序号变字符串。将path中存放的序号所对应的字符串输出

Dijkstra:找cost最少的路径,pre保存每点的前一个节点,易助dfs

dfs:回溯到头,cnt++,判断当前路径sumh、nowavg与maxh、avgh

#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int inf=99999999;
int n,k,sid=0,eid,cnt=0;
int maxh=-inf,avgh=-inf;
int dis[205],hap[205]={0},cost[205][205],vis[205];
vector<int> pre[205],tmpPath,path;
map<string,int> num;
map<int,string> name;
//void getId(string s) {
//	int id=0;
//	for(int i=0; i<s.size(); i++)
//		id=id*26+s[i]-'A';
//	return id;
//}
void dfs(int v) {
	tmpPath.push_back(v);
	if(v==0) {
		cnt++;
		int sumh=0;
		for(int i=0;i<tmpPath.size();i++) //遍历tmpPath 
			sumh+=hap[tmpPath[i]];
		if(sumh>maxh) {
			maxh=sumh;
			avgh=sumh/(tmpPath.size()-1); //分母为个数减一 
			path=tmpPath;
		} else if(sumh==maxh) {
			int nowavg=sumh/(tmpPath.size()-1); //分母为个数减一
			if(avgh<nowavg) {
				avgh=nowavg;
				path=tmpPath;
			}
		}
		tmpPath.pop_back();
		return;
	}
	for(int i=0; i<pre[v].size(); i++)
		dfs(pre[v][i]);
	tmpPath.pop_back();
}
int main() {
	string start,s1,s2;
	int id1,id2,c;
	fill(cost[0],cost[0]+205*205,inf);
	fill(dis,dis+205,inf);
	cin>>n>>k>>start;
	num[start]=0; //起始点永远为0,因为无happy,有happy的点从1到n-1 
	name[0]=start;
	for(int i=1; i<n; i++) {
		cin>>s1;
		num[s1]=i;
		name[i]=s1;
		cin>>hap[i];
	}
	for(int i=0; i<k; i++) {
		cin>>s1>>s2>>c;
		id1=num[s1],id2=num[s2];
		cost[id1][id2]=cost[id2][id1]=c;
	}
	dis[0]=0;
	for(int i=0; i<n; i++) {
		int minn=inf,u=-1;
		for(int j=0; j<n; j++)
			if(vis[j]==0&&dis[j]<minn) minn=dis[j],u=j;
		if(u==-1)break;
		vis[u]=1;
		for(int j=0; j<n; j++) {
			if(vis[j]==0&&cost[u][j]!=inf) {
				if(dis[j]>dis[u]+cost[u][j]) {
					dis[j]=dis[u]+cost[u][j];
					pre[j].clear();
					pre[j].push_back(u);
				} else if(dis[j]==dis[u]+cost[u][j])
					pre[j].push_back(u);
			}
		}
	}
	eid=num["ROM"];
	dfs(eid);
	printf("%d %d %d %d\n",cnt,dis[eid],maxh,avgh);
	int idx=path.size()-1;
	cout<<name[path[idx]];
	for(int i=path.size()-2;i>=0;i--) cout<<"->"<<name[path[i]];
	return 0;
}

1111 Online Map (30 分)

  • n个点,m条路(有单向和双向的两种),求从起点s到终点d的最短路径过程,和最快的路径
  • 两次Dijkstra,一次选最短路(长度相同选速度最快的那个),一次选最快的路(一样快选点少的那个),并分别把节点保存在vector<int> prel [505] 和 pret [505]中
  • 两次dfs,一次挑最短路里最快的那条,一次挑最快路里经过点最少的那条

注意:

  • one-way指单行道
  • 两类变量很相似(下次最好起个区分度大的名字)不要搞混
#include<iostream>
#include<vector>
using namespace std;
const int inf=99999999;
int dis[505],fast[505],vis[505],mpt[505][505],mpl[505][505];
vector<int> prel[505],pret[505],tmp,shortp,fastp;
int mint=inf,minv=inf;
int n,m,s,d;
void dijkstraD() {  //0到size-1,依次从终点到起点,输出得从后往前
	dis[s]=0;
	for(int i=0; i<n; i++) {
		int minn=inf,u=-1;
		for(int j=0; j<n; j++)
			if(vis[j]==0&&dis[j]<minn) minn=dis[j],u=j;
		if(u==-1)break;
		vis[u]=1;
		for(int j=0; j<n; j++) {
			if(vis[j]==0&&mpl[u][j]!=inf) {
				if(dis[j]>dis[u]+mpl[u][j]) {
					dis[j]=dis[u]+mpl[u][j];
					prel[j].clear();
					prel[j].push_back(u);
				} else if(dis[j]==dis[u]+mpl[u][j])
					prel[j].push_back(u);
			}
		}
	}
}
void dijkstraT() {
	fast[s]=0;
	for(int i=0; i<n; i++) {
		int minn=inf,u=-1;
		for(int j=0; j<n; j++)
			if(vis[j]==0&&fast[j]<minn) minn=fast[j],u=j;
		if(u==-1)break;
		vis[u]=1;
		for(int j=0; j<n; j++) {
			if(vis[j]==0&&mpt[u][j]!=inf) {
				if(fast[j]>fast[u]+mpt[u][j]) {
					fast[j]=fast[u]+mpt[u][j];
					pret[j].clear();
					pret[j].push_back(u);
				} else if(fast[j]==fast[u]+mpt[u][j])
					pret[j].push_back(u);
			}
		}
	}
}
void dfsL(int v) {
	tmp.push_back(v);
	if(v==s) {
		int sumt=0,a,b;
		for(int i=0; i<tmp.size()-1; i++) {
			a=tmp[i];
			b=tmp[i+1];
			sumt+=mpt[b][a];
		}
		if(sumt<mint) {
			mint=sumt;
			shortp=tmp;
		}
		tmp.pop_back();
		return;
	}
	for(int i=0; i<prel[v].size(); i++)
		dfsL(prel[v][i]);
	tmp.pop_back();
}
void dfsT(int v) {
	tmp.push_back(v);
	if(v==s) {
		int sumv=tmp.size();
		if(sumv<minv) {
			minv=sumv;//
			fastp=tmp;
		}
		tmp.pop_back();
		return;
	}
	for(int i=0; i<pret[v].size(); i++)
		dfsT(pret[v][i]);
	tmp.pop_back();
}
int main() {
	fill(mpl[0],mpl[0]+505*505,inf);
	fill(mpt[0],mpt[0]+505*505,inf);
	fill(dis,dis+505,inf);
	fill(fast,fast+505,inf);
	cin>>n>>m;
	int a,b,dir,l,t;
	for(int i=0; i<m; i++) {
		cin>>a>>b>>dir>>l>>t;
		mpl[a][b]=l,mpt[a][b]=t;
		if(dir==0) mpl[b][a]=l,mpt[b][a]=t; //one-way 单向的 
	}
	cin>>s>>d;
	dijkstraD();
	dfsL(d);
	fill(vis,vis+505,0);    //共用一个vis,第二次得清空
	tmp.clear();
	dijkstraT();
	dfsT(d);
	if(shortp==fastp) {
		printf("Distance = %d; Time = %d: %d",dis[d],fast[d],s);
		for(int i=shortp.size()-2; i>=0; i--) printf(" -> %d",shortp[i]);
	} else {
		printf("Distance = %d: %d",dis[d],s);
		for(int i=shortp.size()-2; i>=0; i--) printf(" -> %d",shortp[i]);
		printf("\nTime = %d: %d",fast[d],s);
		for(int i=fastp.size()-2; i>=0; i--) printf(" -> %d",fastp[i]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值