关闭

1087. All Roads Lead to Rome (30) PAT

标签: Dijkstradfs
141人阅读 评论(0) 收藏 举报
分类:

最短路径和dfs,先用dijkstra找出最短路径,然后用深搜找出相同路径的数量以及最佳答案;可以用map来标注city

变量设的略乱,囧!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
int N,K;
map<string,int>m;//标记城市名字
map<int,string>mm;
int G[205][205];
int h[205],vis[205],dis[205];//每个城市的happyness,标记数组,最短路径标记数组
int route,num_route,avg_h=999999,happy;//最短路径,最短路径的数量,平均happyness,最终路径的happyness
vector<int>res,ans;//保存经过的城市
void dij(){
	vis[0]=1;
	for(int i=0;i<N;i++){
		if(G[0][i]!=0){
			dis[i]=G[0][i];
		}
	}
	for(int i=1;i<N;i++){
		int minn=999999;
		int s;
		for(int i=0;i<N;i++){
			if(minn>dis[i]&&vis[i]==0){
				minn=dis[i];
				s=i;
			}
		}
		vis[s]=1;
		for(int i=0;i<N;i++){
			if(dis[i]>dis[s]+G[s][i]&&G[s][i]!=0){
				dis[i]=dis[s]+G[s][i];
			}
		}
	}
	return ;
}

void dfs(int rou,int route,int end,int s,int num,int ha){
	if(rou>route){
		return ;
	}
	if(s==end){
		if(rou!=route){
			return ;
		}
		num_route++;
		double tmp=ha/num;
		if(ha>happy){
			happy=ha;
			ans=res;
			avg_h=tmp;
		}
		else if(ha==happy){
			if(avg_h<tmp){
				avg_h=tmp;
				ans=res;
				happy=ha;
			}
		}
	}
	for(int i=0;i<N;i++){
		if(vis[i]==0&&G[s][i]!=0){
			vis[i]=1;
			res.push_back(i);
			dfs(G[s][i]+rou,route,end,i,num+1,ha+h[i]);
			res.pop_back();
			vis[i]=0;
		}
	}
}

int main(){
	scanf("%d%d",&N,&K);
	string city,city1,city2;
	int e;
	int cost,hap;
	memset(G,0,sizeof(G));
	memset(h,0,sizeof(h));
	cin>>city;
	m[city]=0;
	mm[0]=city;
	for(int i=1;i<=N-1;i++){
		cin>>city>>hap;
		m[city]=i;
		mm[i]=city;
		if(city=="ROM"){
			e=i;
		}
		h[i]=hap;
	}
	for(int i=0;i<K;i++){
		cin>>city1>>city2>>cost;
		G[m[city1]][m[city2]]=G[m[city2]][m[city1]]=cost;
	}
	memset(vis,0,sizeof(vis));
	memset(dis,999999,sizeof(dis));
	dij();
	route=dis[e];
	memset(vis,0,sizeof(vis));
	dfs(0,route,e,0,0,0);
	printf("%d %d %d %d\n",num_route,route,happy,avg_h);
	cout<<mm[0];
	for(int i=0;i<ans.size();i++){
		cout<<"->"<<mm[ans[i]];
	}
	cout<<endl;
	return 0;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

React-Native使用小发现

5、在创建自定义原生模块时,命名一定要注意,不能带RCT前缀。RCT前缀适用于原生View,搭配RCT***Manager一起使用,可能是RN内部设置。 6、在实际开发中,IOS使用模拟器调试时,经常...
  • pz789as
  • pz789as
  • 2016-10-15 16:51
  • 2057

警告:Block的Retain Cycle的解决方法

警告:Captureing ‘self’ strongly in this block is likely to lead to a retain cycle 一个使用Block语法的实例变量,...
  • itianyi
  • itianyi
  • 2013-03-25 10:45
  • 12041

pat-1087【最短路径】

最短路径
  • linsheng9731
  • linsheng9731
  • 2014-10-20 09:04
  • 1254

[PAT]1087. All Roads Lead to Rome (30)

/************************************************************** 1087. All Roads Lead to Rome (30) 时...
  • fnzsjt
  • fnzsjt
  • 2014-11-26 22:42
  • 1451

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息1087. All Roads Lead to Rome (30)时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Indeed there are many ...
  • xianyun2009
  • xianyun2009
  • 2016-05-14 20:08
  • 3230

PAT 1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 ...
  • frozenshore
  • frozenshore
  • 2015-09-06 16:11
  • 372

pat 1087. All Roads Lead to Rome (30)

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
  • hyf20144055065
  • hyf20144055065
  • 2015-12-21 16:03
  • 424

pat 1087. All Roads Lead to Rome (30) 解答

这题和之前一题紧急救援有点像 这里用的变形的dijkstra 每个节点设置一个vector,遇到相同最短路的,就在vector记录来自哪个节点; 这样 dijkstra结束后,就可以从终点往前推出 所...
  • var1994
  • var1994
  • 2016-09-07 17:28
  • 188

pat 1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30)
  • nan327347465
  • nan327347465
  • 2014-09-06 19:17
  • 1858

1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS

1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city ...
  • liuchuo
  • liuchuo
  • 2016-09-09 13:26
  • 226
    个人资料
    • 访问:19446次
    • 积分:910
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论
  • ccf 俄罗斯方块

    oxygen_czp: 楼主这个算法只能说是投机取巧,不能说是正确的 算法虽然能够得100 但我已经发现两处有问题1:当...