Seaside HDU 3665 【Dijkstra】
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.
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.
5
1 0
1 1
2 0
2 3
3 1
1 1
4 100
0 1
0 1
2
//Dijkstra
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
int N;
int n;
int low[20];
bool vis[20];
int map[20][20];
void Dijkstra()
{
int pos=0;
int i,j;
memset(vis,0,sizeof(vis));
memset(low,INF,sizeof(low));
for(i=0;i<n;++i)
{
low[i]=map[0][i];
}
vis[0]=1;
low[0]=0;
for(i=0;i<n-1;++i)
{
int min=INF;
for(j=0;j<n;++j)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
pos=j;
}
}
vis[pos]=1;
for(j=0;j<n;++j)
{
if(!vis[j]&&low[j]>map[pos][j]+low[pos])
low[j]=map[pos][j]+low[pos];
}
}
printf("%d\n",low[n-1]);
}
int main()
{
int i, j;
int M, flag;
int u,v,w;
while(~scanf("%d", &N))
{
n=N+1;
for(i=0;i<=N;++i)
for(j=0;j<=i;++j)
map[i][j]=map[j][i]=INF;
//memset(map,INF,sizeof(map));
for(int i = 0; i < N; ++i)
{
scanf("%d%d", &M, &flag);
if(flag) map[i][n-1]=map[n-1][i]=0;
while(M--)
{
scanf("%d%d",&v,&w);
if(map[i][v]>w)
map[i][v]=map[v][i]=w;
}
}
Dijkstra();
}
}
Seaside HDU 3665 【Dijkstra】的更多相关文章
- 【Dijkstra】
[摘自]:华山大师兄,推荐他的过程动画~ myth_HG 定义 Dijkstra算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩 ...
- How far away ? HDU - 2586 【LCA】【RMQ】【java】
题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最 ...
- HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划
有一个只含0和1的长度为n的串,问不含有101的所有串的个数. ——不存在连续的101.010.111的字符串数量 HDU:https://cn.vjudge.net/problem/HDU-3485 ...
- HDU 3790 最短路径问题【Dijkstra】
题意:给出n个点,m条边,每条边的长度d和花费p,给出起点和终点的最短距离和花费,求最短距离,如果有多个最短距离,输出花费最少的 在用dijkstra求最短距离的时候,再用一个f[]数组保存下最少花费 ...
- HDU 2066 一个人的旅行【Dijkstra 】
题意:给出s个起点,d个终点,问从这些起点到达终点的最短距离 因为有多个起点,所以把这多个起点的值设为0 哎= =改了好久的说= = 是因为在代码里面的t,不知道为什么调用dijkstra()函数之后 ...
- HDU 4370 0 or 1 (01规划)【Dijkstra】||【spfa】
<题目链接> 题目大意: 一个n*n的01矩阵,满足以下条件 1.X12+X13+...X1n=12.X1n+X2n+...Xn-1n=13.for each i (1<i<n ...
- hdu 3499 flight 【分层图】+【Dijkstra】
<题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...
- POJ1797 Heavy Transportation 【Dijkstra】
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 21037 Accepted: ...
- poj 2502 Subway【Dijkstra】
<题目链接> 题目大意: 某学生从家到学校之间有N(<200)条地铁,这个学生可以在任意站点上下车,无论何时都能赶上地铁,可以从一条地铁的任意一站到另一条地跌的任意一站,学生步行速度 ...
随机推荐
- Codeforces--630C--Lucky Numbers(快速幂)
C - Lucky Numbers Crawling in process... Crawling failed Time Limit:500MS Memory Limit:65536K ...
- UESTC--1251--谕神的密码(贪心)
谕神的密码 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit Status ...
- CALayer(一)
CALayer CALayer和UIView CALayer和UIView相比--CALayer少了事件处理的功能,所以更加轻量级,性能更好一点,这就说明如果有一些和用户交互的东西是不建议用CALay ...
- HTML中javascript使用dom获取dom节点范例
<!-- HTML结构 --> <div id="test-div"> <div class="c-red"> <p ...
- 消除svn选定(checkout)桌面上文件显示一大堆问号。
图片: 解决方法一: 桌面右键选择TortoiseSVN——>点击Settings,如下图,选中Icon Overlays(图标覆盖),去勾选Fixed drives(本地磁盘),点击确定,按F ...
- 轻量数据库SQLiteDataBase的相关操作方法
一.查询操作: 查询操作比较复杂,主要有如下操作: db.rawQuery(String sql, String[] selectionArgs); db.query(String table, St ...
- Linux之tar.gz file
A tarball (tar.gz file) is compressed tar archive. The tar program provides the ability to create ta ...
- MVC5+EasyUI+EF6+Linq通用权限系统出炉--登录(2)
1.输入验证码后 自动识别验证码并登录.
- 黑客常用dos命令
http://blog.csdn.net/CSDN___LYY/article/details/77802438
- 安卓代码迁移:Program "sh" not found in PATH
Description Resource Path Location Type Program "sh" not found in PATH 参考链 ...