时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:870

解决:415

题目描述:

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年北京大学计算机研究生机试真题

思路:

题目大意是N个城市分属于两个敌对集团,要从城市1到城市2(分别属于两个集团),只能有一条路跨集团,求最短路径。

我的思路是,求城市1到其集团中其它城市的最短路径,城市2也同样,然后对集团间存在的路径i到j,求d(1,i)+d(i,j)+d(j,2)的最小值。

代码:

#include <stdio.h>

#define N 600
#define M 10000
#define INF 1e8 int n;
int D[N][N];
int support[N], visit[2][N], dis[2][N]; void init()
{
for (int i=0; i<n; i++)
{
support[i] = 0;
visit[0][i] = visit[1][i] = 0;
dis[0][i] = dis[1][i] = INF;
for (int j=0; j<n; j++)
{
D[i][j] = INF;
}
}
} void printdis(int s)
{
int i;
for (i=0; i<n; i++)
{
if (support[i] == s)
printf("%d\n", dis[s][i]);
}
printf("\n");
} void dijkstra(int s)
{
int i, j;
for (i=0; i<n; i++)
{
if (support[i] == s)
dis[s][i] = D[s][i];
}
dis[s][s] = 0;
visit[s][s] = 1;
//printdis(s); int mind;
int k;
for (i=0; i<n; i++)
{
mind = INF;
for (j=0; j<n; j++)
{
if ( support[j] == s && !visit[s][j] && (dis[s][j]<mind) )
{
mind = dis[s][j];
k = j;
}
}
if (mind == INF)
break;
visit[s][k] = 1;
for (j=0; j<n; j++)
{
if ( support[j] == s && !visit[s][j] && (dis[s][k]+D[k][j] < dis[s][j]) )
{
dis[s][j] = dis[s][k]+D[k][j];
}
}
}
//printdis(s);
} int Min(int a, int b)
{
return (a<b) ? a : b;
} int main(void)
{
int m, i, j;
int a, b, d;
int min; while (scanf("%d", &n) != EOF && n)
{
init();
scanf("%d", &m);
for(i=0; i<m; i++)
{
scanf("%d%d%d", &a, &b, &d);
D[a-1][b-1] = D[b-1][a-1] = d;
}
for(i=0; i<n; i++)
{
scanf("%d", &a);
support[i] = a-1;
} dijkstra(0);
dijkstra(1); min = INF;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (support[i] == 0 && support[j] == 1)
{
min = Min(dis[0][i] + D[i][j] + dis[1][j], min);
}
}
}
if (min == INF)
printf("-1\n");
else
printf("%d\n", min);
} return 0;
}
/**************************************************************
Problem: 1162
User: liangrx06
Language: C
Result: Accepted
Time:20 ms
Memory:2336 kb
****************************************************************/

最新文章

  1. 几种鼠标触发CSS事件
  2. rabbitmq method之basic.consume
  3. .net实现微信公众账号接口开发
  4. 前端开发必须知道的JS(二) 闭包及应用
  5. oracle-12c-rac 报:ORA-01078
  6. css块级元素、行内元素
  7. 关于简明Vim练级攻略
  8. Java 输入
  9. HDOJ 1312题Red and Black
  10. VS Code开发调试ASP.NET Core 1.0
  11. [Erlang危机](4.5)第四章练习
  12. CentOS下实用的网络管理工具
  13. layui时间控件,获取页面选中的时间值。
  14. 机器学习--k-means聚类原理
  15. JS 使用const声明常量的本质(很多人都有误解)
  16. 【笔记】Cocos2dx学习笔记
  17. Jenkins持续集成学习-Windows环境进行.Net开发1
  18. MPD软件工作坊北京站:技术创新与研发效率带来的前沿思考
  19. Eloquent JavaScript #13# HTTP and Forms
  20. flask框架get post方式

热门文章

  1. Objective-C的self.用法的一些总结
  2. weblogic的集群与配置图文方法
  3. Zend Studio 9.0.2破解文件和注册码下载
  4. apache服务器日志及重启方法
  5. cordova 中de.sitewaerts.cordova.documentviewer 插件 看pdf图片缩略图与实际图片不一致
  6. spark-submit提交方式测试Demo
  7. from: Java开发必须要知道的知识体系
  8. PGM图片格式与代码
  9. struts2类型转换+校验
  10. 【DB2】DB2使用IMPORT命令导入含有自增长列的表报错处理