九度OJ 1162 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
样例输出:
100
90
540
/*********************************
邻接表的代码
其实用邻接矩阵会简单点......
**********************************/
#include 
#include 

using namespace std;

const int INF = 0x7FFFFFFF;
struct E
{
	int next;
	int cost;
};
vector edge[610];


bool mark[610];
int dist[610];

int main()
{
	int N, M;
	//freopen("1162in.txt","r",stdin);
	while(scanf("%d",&N) != EOF && N)
	{
		scanf("%d",&M);
		int i, j;
		for(i = 1; i <= N; i++)
		{
			edge[i].clear();
			dist[i] = -1;
			mark[i] = false;
		}
		for(i = 0; i < M; i++)              //构造邻接表
		{
			int A, B, T;
			scanf("%d%d%d",&A,&B,&T);
			E tmp;
			tmp.next = B;
			tmp.cost = T;
			edge[A].push_back(tmp);
			tmp.next = A;
			edge[B].push_back(tmp);
		}
		int support[610];
		for(i = 1; i <= N; i++)
			scanf("%d",&support[i]);

		dist[1] = 0;
		mark[1] = true;
		int newP = 1;
		for(i = 1; i < N; i++)               //dijkstra
		{
			for(j = 0; j < edge[newP].size(); j++)
			{
				int t = edge[newP][j].next;
				int c = edge[newP][j].cost;
				if(mark[t] == true)
					continue;
				if(support[newP] == 2 && support[t] == 1) // 若是从阵营2到阵营1的路则跳过即可
					continue;
				if(dist[t] == -1 || dist[newP] + c < dist[t])
					dist[t] = dist[newP] + c;
			}
			int min = INF;
			for(j = 1; j <= N; j++)
			{
				if(mark[j] == true || dist[j] == -1)
					continue;
				if(dist[j] < min)
				{
					min = dist[j];
					newP = j;
				}
			}
			mark[newP] = true;
		}
		printf("%d\n",dist[2]);
	}
	return 0;
}


你可能感兴趣的:(九度OJ)