HDU-3665 Seaside
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 S Mi and L Mi, which means that the distance between the i-th town and the S Mi town is L Mi.
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
最短路径,用迪杰斯特拉或者SPFA;
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 10 + 5;
const int INF = (1<<28);
int mat[N][N],D[N];
bool visit[N],near_seaside[N];
void Init(){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++) mat[i][j] = INF;
D[i] = INF,visit[i] = near_seaside[i] = false;
}
}
int solve(const int n){
int cnt,i,k;
D[0] = 0;
for(cnt = 1;cnt<=n;cnt++){
for(k=-1,i=0;i<n;i++)
if(!visit[i]&&(k==-1||D[i] <D[k])) k = i;
for(visit[k]=true,i=0;i<n;i++)
if(!visit[i]&&D[i] > D[k]+mat[k][i])
D[i] = D[k] + mat[k][i];
}
int ans = INF;
for(int i=0;i<n;i++)
if(near_seaside[i]) ans = min(ans,D[i]);
return ans;
}
void Input_data(const int n){
int u,v,c;
for(int i=0;i<n;i++){
scanf("%d %d",&u,&c);
if(c) near_seaside[i] = true;
for(int j=0;j<u;j++){
scanf("%d %d",&v,&c);
if(c < mat[i][v]) mat[i][v] = mat[v][i] = c;
}
}
}
int main(){
int n;
while(scanf("%d",&n)==1){
Init();
Input_data(n);
printf("%d\n",solve(n));
}
}
最新文章
- 如何在WIN7下进行LINUX虚拟机搭建
- js控制html文字提示语的出现和隐藏
- 打包并发布自己的Android应用(eclipse)
- UITextField和一个UILabel绑定 浅析
- 图表工具--- ECharts.js学习(一) 简单入门
- webview缓存及跳转时截取url地址、监听页面变化
- Java 线程池(ThreadPoolExecutor)原理分析与使用
- 【English】九、kids/children/toddlers 三个单词的区别
- cmd命令窗口的快速选中复制黏贴
- java中的try-catch-finally异常处理(学习笔记)
- Windows Community Toolkit 3.0 - CameraPreview
- 个人阅读作业WEEK7 (软件工程的瀑布, 大泥球, 教堂,集市,和银弹)
- 自学Aruba4.1-Aruba开机初始化
- String StringBuffer和StringBuilder区别及性能
- LOJ.2863.[IOI2018]组合动作(交互)
- C#编程(六十)----------LINQ的概述
- array string mysql IN LIKE
- 使用 SendARP 获取 MAC 地址(使用SendARP API函数,很多相关文章)
- CDN和镜像站点比较
- C#设置窗体中的窗体随主窗体大小变化而变化