I Wanna Go Home 迪杰斯特拉 研究生考研真题

I Wanna Go Home 迪杰斯特拉 研究生考研真题

The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible. “For the sake of safety,”, said Mr.M, “your route should contain at most 1 road which connects two cities of different camp.” Would you please tell Mr. M at least how long will it take to reach his sweet home?

The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.

For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M’s demands, output -1 instead.

2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0

#include<iostream>
#include<queue>
#include<climits>
#include<cstring>
#include<vector>
using namespace std;

const  int MAXN=650;
const  int INF=INT_MAX;

int home[650];//leader of cities
int n,m;

int dis[MAXN];//


struct To_Eage{  //
	
	int to;
	int length;
	
	
	To_Eage(int t,int l):to(t),length(l){
	}
};


struct Num_Distance{
	int number;
	int distance;
	Num_Distance(int n,int d):number(n),distance(d){
	}
	
	
	bool operator <(const Num_Distance & mm) const{
		
		return distance>mm.distance;
		
	}
};

vector<To_Eage> pp[MAXN];

priority_queue<Num_Distance> myqueue;//大顶锥,优先级高的先输出


void dijs()
{
	dis[1]=0;
	myqueue.push(Num_Distance(1,0));
	
	while(!myqueue.empty())
	{
		int u=myqueue.top().number;
		myqueue.pop();
		
		for(int i=0;i<pp[u].size();i++)
		{
			int v=pp[u][i].to;
			int length=pp[u][i].length;
			if(!(home[u]==2 &&home[v]==1) && dis[v]>dis[u]+length)//only not ok 2 to 1
			{
				dis[v]=dis[u]+length;
				myqueue.push(Num_Distance(v,dis[v]));
			}
		}
		
	}
}

int main()
{
	while(scanf("%d",&n)==1 &&n)
	{
		cin>>m;
		fill(dis,dis+n+1,INF);
		memset(home,0,sizeof(home));
		memset(pp,0,sizeof(pp));
		int x,y;
		int length;
		for(int i=0;i<m;i++)
		{
			cin>>x>>y>>length;
			pp[x].push_back(To_Eage(y,length));
			pp[y].push_back(To_Eage(x,length));
			
		}
		
		for(int j=1;j<=n;j++)
		{
			cin>>home[j];
		}
		
		
		dijs();
		
		if(dis[2]==INF)
			cout<<-1<<endl;
		else cout<<dis[2];
		
	}
	
	
	return 0;
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值