您的位置:首页 > 编程语言 > Go语言

题目1162:I Wanna Go Home

2017-02-17 15:14 357 查看
题目描述:

    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
4000
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<stdio.h>
#include<vector>
#define INF 0x7fffffff
using namespace std;
struct Edge {
int next;
int cost;
};
vector<Edge> edges[601];
int a[10001], b[10001], c[10001];
int camp[601];
int dis[601];
int mark[601];

int main() {
int n, m;
while (scanf("%d", &n) != EOF) {
if (n == 0)
break;
scanf("%d", &m);
for (int i = 0; i < m; i++)
scanf("%d%d%d", &a[i], &b[i], &c[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &camp[i]);
for (int i = 1; i <= n; i++)
edges[i].clear();
for (int i = 0; i < m; i++) {
int x = a[i];
int y = b[i];
if (camp[x] != 1) {
int tmp = x;
x = y;
y = tmp;
}
Edge edge;
edge.cost = c[i];
edge.next = y;
edges[x].push_back(edge);
if (camp[x] == camp[y]) {
edge.next = x;
edges[y].push_back(edge);
}
}
for (int i = 1; i <= n; i++) {
dis[i] = INF;
mark[i] = false;
}
int newp = 1;
dis[newp] = 0;
mark[newp] = true;
for (int j = 1; j < n; j++) {
for (int i = 0; i < edges[newp].size(); i++) {
int next = edges[newp][i].next;
int cost = edges[newp][i].cost;
if (mark[next] == true)
continue;
if (dis[next] > dis[newp] + cost)
dis[next] = dis[newp] + cost;
}
int min = INF;
for (int i = 1; i <= n; i++) {
if (mark[i])
continue;
if (dis[i] < min) {
min = dis[i];
newp = i;
}
}
mark[newp] = true;
}
if (dis[2] != INF)
printf("%d\n", dis[2]);
else
printf("-1\n");
}
return 0;
}


题目链接: http://ac.jobdu.com/problem.php?pid=1162
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: