其他分享
首页 > 其他分享> > 九度OJ题目1162-I Wanna Go Home

九度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

题目大意:

每组数据的最后一行的数字,表示第i个结点的头领数,不同数字代表不同的营地,题目要求最短路径中,最多只有一条路径是连接不同的营地。或者说1代表圆形,2表示方型,最短路径中最多只有一条路径连接方型和圆形。

/*因为1结点是1类型,2结点为2类型,无论其他结点是什么类型,(若存在)肯定有一条路径是连接不同类型,因此,

这里设置2类型到1类型为不可达(可以做标记或者删除该边),总体的图模型既包含无向边,有包含有向边*/
参考代码:

/*带有约束条件的dijstra算法*/

#include<cstdio>
#include<vector>
#include<iostream>
using namespace std;
#define N 601
#define M 10001
struct E {
	int next;
	int c;
	//int flag;
	int ans;//标记城市类型是否一样
};
vector<E> edge[N];
int dis[N];
int mark[N];
//int cost[N];
int flag[N];//记录城市类型
int main() {
	int n, m;
	while (scanf("%d", &n) != EOF) {
		scanf("%d", &m);
		for (int i = 1; i <=n; i++) edge[i].clear();
		for (int i = 0; i < m; i++) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			E tmp;
			tmp.c = c;
			tmp.ans = 0;
			tmp.next = b;
			edge[a].push_back(tmp);
			tmp.next = a;
			edge[b].push_back(tmp);
		}
		for (int i = 1; i <=n; i++) {//记录第i个结点的类型
			scanf("%d", &flag[i]);
		}

		for (int i = 1; i <= n; i++) {//将2类型到1类型的城市设为不可达
			for (int j = 0; j < edge[i].size(); j++) {
				if (flag[i] == 2) {
					int ans = edge[i][j].next;
					if (flag[ans] == 1) {
						edge[i][j].ans = -1;
						//edge[i].erase(edge[i].begin() + ans, edge[i].begin() + ans + 1);//删除该节点
					}
				}
			}
		}
		//使用dijkstra算法求最短路径
		for (int i = 1; i <= n; i++) {
			dis[i] = -1;
			mark[i] = 0;
		}
		dis[1] = 0;//起点
		mark[1] = 1;
		int newP = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < edge[i].size(); j++) {
				int t = edge[i][j].next;
				int p = edge[i][j].c;
				if (mark[t] == 1|| edge[i][j].ans == -1 ) continue;//
				if (dis[t] == -1 || dis[t] > dis[newP] + p) {
					dis[t] = dis[newP] + p;
				}
			}
			int min = 123123123;
			for (int j = 1; j <= n; j++) {
				if (mark[j] == true) continue;
				if (dis[j] == -1) continue;
				if (dis[j] < min) {
					newP = j;
					min = dis[j];
				}
			}
			mark[newP] = 1;
		}
		printf("%d\n", dis[2]);
	}

	return 0;
}

 

标签:city,OJ,1162,int,九度,Mr,home,integer,dis
来源: https://blog.csdn.net/sinat_38292108/article/details/88343472