时间限制: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
****************************************************************/

九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)的更多相关文章

  1. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  2. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  3. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  4. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  5. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  6. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  7. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  8. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  9. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. Java线程池原理解读

    引言 引用自<阿里巴巴JAVA开发手册> [强制]线程资源必须通过线程池提供,不允许在应用中自行显式创建线程. 说明:使用线程池的好处是减少在创建和销毁线程上所消耗的时间以及系统资源的开销 ...

  2. 2016集训测试赛(二十四)Problem B: Prz

    Solution 这道题有两个关键点: 如何找到以原串某一个位置为结尾的某个子序列的最晚出现位置 如何找到原串中某个位置之前的所有数字的最晚出现位置中的最大值 第一个关键点: 我们注意到每个数字在\( ...

  3. laravel 性能提升

    php artisan optimize 相当于: 1.composer dump-autoload --optimize // composer 层面优化加载速度 2.php artisan cle ...

  4. ZFS -世界上最高级的文件系统之一

    https://www.oschina.net/news/44302/openzfs_launch_announcement https://en.wikipedia.org/wiki/ZFS ZFS ...

  5. AMD 的 CommonJS wrapping

    其实本文的标题应该是「为什么我不推荐使用 AMD 的 Simplified CommonJS wrapping」,但太长了不好看,为了美观我只能砍掉一截. 它是什么? 为了复用已有的 CommonJS ...

  6. hdu1420(C++)

    数论中模的运算: a*b%n=(a%n)*(b%n)%c; (a+b)%n=(a%n+b%n)%n; 幂的模:A^n%c=r    于是A^(n+1)%c=A*r%c; #include<ios ...

  7. API测试工具postman使用总结

    一.Postman介绍: Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件,主要用于模拟网络请求包,快速创建请求,回放.管理请求,验 ...

  8. Java数据结构-线性表之静态链表

    静态链表的定义: 节点由一个一维数组和一个指针域组成,数组用来存放数据元素,而指针域里面的指针(又称游标)用来指向下一个节点的数组下标. 这种链表称之为静态链表. 链表中的数组第一个和最后一个位置须要 ...

  9. java命令行

    Launches a Java application. Synopsis java [options] classname [args] java [options] -jar filename [ ...

  10. UNP学习笔记(第十四章 高级I/O函数)

    本章讨论我们笼统地归为“高级I/O”的各个函数和技术 套接字超时 有3种方法在涉及套接字的I/O操作上设置超时 1.调用alarm,它在指定超时时期满时产生SIGALRM信号 2.在select中阻塞 ...