首页 > 代码库 > HDU 3665 Seaside
HDU 3665 Seaside
题目链接:
http://acm.split.hdu.edu.cn/showproblem.php?pid=3665
Problem Description
XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.
Input
There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that the distance between the i-th town and the SMi town is LMi.
Output
Each case takes one line, print the shortest length that XiaoY reach seaside.
Sample Input
5
1 0
1 1
2 0
2 3
3 1
1 1
4 100
0 1
0 1
Sample Output
2
Hint:
题意:
一个含有N个小城镇的大城镇,以0到N-1的顺序,先给出两个数a,b;表示i到其他城镇有a条道路,b表示该地方是否有海,0表示没有海,1表示有海。
然后在给出a1,x;a1表示i到a1之间有道路,距离是x。求从0到有海的城市的最小的距离。
题解:
简单的最短路问题。
代码:
#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10+10;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fint map[maxn][maxn];int visited[maxn],dis[maxn];int num[maxn];int n,m;void dijkstra(int x){ int min=0,p=0; for(int i=0;i<n;i++) { dis[i]=map[x][i]; visited[i]=0; } visited[x]=1; for(int i=0;i<n;i++) { min=inf; for(int j=0;j<n;j++) { if(!visited[j]&&dis[j]<min) { min=dis[j]; p=j; } } visited[p]=1; for(int j=0;j<n;j++) { if(!visited[j]&&dis[p]+map[p][j]<dis[j]) dis[j]=dis[p]+map[p][j]; } }}int main(){ while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) map[i][j]=inf; met(num,0); for(int i=0;i<n;i++) { scanf("%d%d",&m,&num[i]); for(int j=0;j<m;j++) { int x,y; scanf("%d%d",&x,&y); map[i][x]=map[x][i]=y; } } dijkstra(0); int ans=inf; for(int i=0;i<n;i++) { if(num[i]==1) ans=min(ans,dis[i]); } printf("%d\n",ans); }}
HDU 3665 Seaside
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。