九度OJ-1162:I Wanna Go Home

  使用的依然是Dijkstra算法的模板。略作修改即可

题目描述:

    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
样例输出:
100
90
540
来源:
2011年北京大学计算机研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-7885-1-1.html

#include <iostream>
#include <vector>
#define MAXSIZE 600 
#define MAXSIZE_EDGE 10000
using namespace std;
struct Edge{
	int start,end;
	double weight;
	Edge(){
	}
	Edge(int start,int end,double weight){
		this->start=start;
		this->end=end;
		this->weight=weight;
	}
	bool operator<(const Edge e){//useless
		return weight<e.weight;
	}
}; 

struct Vex{
	int root;
	Vex(){//initiate root
		this->root=-1;
	} 
};
struct Graph{ //顶点下标从0开始 
	int graphSize;
	vector<Edge> edge[MAXSIZE_EDGE];
	int initGraph(int graphSize){
		this->graphSize=graphSize;
		for (int i=0;i<graphSize;i++)
			edge[i].clear();
	}
};
int main(){
	int n,m;
	Graph graph;
	int start,end,weight;
	bool mark[MAXSIZE];
	int dist[MAXSIZE];
	int conversion[MAXSIZE]; 
	int support[MAXSIZE];
	int newPoint,minDistVex;
	int temp,tempCon;
	while (cin>>n,n){//n vexes ,m edges
		cin>>m;
		//initiate
		graph.initGraph(n);
		newPoint=0;//originate 0
		for (int i=0;i<graph.graphSize;i++){
			mark[i]=false;
			dist[i]=-1;
			conversion[i]=0;
		}
		mark[newPoint]=true;
		dist[newPoint]=0;
		//input edge&&support
		for (int i=0;i<m;i++){
			cin>>start>>end>>weight;
			start--;end--;
			graph.edge[start].push_back(Edge(start,end,weight));
		 
			graph.edge[end].push_back(Edge(end,start,weight));
		
		}
		for (int i=0;i<graph.graphSize;i++){
			cin>>support[i];
		}
		//process
		for (int time=0;time<n-1;time++){//每趟循环找出x到一个结点的最短路径,共n-1趟 
			//遍历newPoint直接相邻的结点,修改其dist
			for (int i=0;i<graph.edge[newPoint].size();i++){
				int end=graph.edge[newPoint][i].end;
				if (mark[end]==true)//if end结点已经在x集合中,则跳过此次循环 
					continue;
				tempCon=conversion[newPoint]+(support[end]==support[newPoint]?0:1);//if 转向将要大于一次 continue 
				if (tempCon>1)
					continue;
				temp=dist[newPoint]+graph.edge[newPoint][i].weight;
				if (temp<dist[end]||dist[end]==-1) {//if the new dist< the old dist,modify the path
					dist[end]=temp;
					conversion[end]=tempCon;
				}
			}	
			//遍历dist,从mark为false的结点中找出其dist最小的结点,确定为新的newPoint,并加入x集合 
			int i;
			for (i=0;i<graph.graphSize;i++){//initiate minDistVex
				if (mark[i]==false&&dist[i]!=-1){
					minDistVex=i;
					break;
				}
			}
			for (i++;i<graph.graphSize;i++){
				if (mark[i]==false&&dist[i]!=-1&&dist[i]<dist[minDistVex])
					minDistVex=i;
			} 
			newPoint=minDistVex;
			mark[minDistVex]=true;
		} 
		//output. output dist[]
			cout<<dist[1]<<endl;
		
	}
	return true;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值