Light oj 1002 Country Roads (Dijkstra)
题目连接:
http://www.lightoj.com/volume_showproblem.php?problem=1002
题目描述:
有n个城市,从0到n-1开始编号,n个城市之间有m条边,中心城市为t,问每个城市到中心城市的最小路径的花费,路径花费大小的定义为:一条路上花费最大的边的值。
解题思路:
Dijkstra的变形,用Dijkstra求出来的单源路径可以保证每条边都是最优的,所以最短路上的最长边就是所求。
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
int map[maxn][maxn], dist[maxn];
void init ()
{
int i, j;
for (i=; i<maxn; i++)
for (j=; j<maxn; j++)
if (i==j)
map[i][j] = ;
else
map[i][j] = INF;
}
void dijkstra (int s, int n)
{
int i, j, vis[maxn];
memset (vis, , sizeof(vis)); for (i=; i<n; i++)
dist[i] = map[s][i];
vis[s] = ; for (i=; i<n; i++)
{
int mini = INF, index;
for (j=; j<n; j++)
if (!vis[j] && mini > dist[j])
{
mini = dist[j];
index = j;
}
if (mini == INF)
return ;
vis[index] = ;
for (j=; j<n; j++)
if (!vis[j])
{
dist[j] = min(dist[j], max(map[index][j],dist[index]));//重点
}
}
}
int main ()
{
int T, n, m, l = ;
scanf ("%d", &T);
while (T --)
{
init ();
scanf ("%d %d", &n, &m);
while (m --)
{
int u, v, w;
scanf ("%d %d %d", &u, &v, &w);
if (map[u][v] > w)//有重边,选最优
map[u][v] = map[v][u] = w;
}
int t;
scanf ("%d", &t);
dijkstra (t, n);
printf ("Case %d:\n", l ++);
for (int i=; i<n; i++)
if (dist[i] != INF)
printf ("%d\n", dist[i]);
else
printf ("Impossible\n");
}
return ;
}
Light oj 1002 Country Roads (Dijkstra)的更多相关文章
- 1002 - Country Roads(light oj)
1002 - Country Roads I am going to my home. There are many cities and many bi-directional roads betw ...
- Lightoj 1002 - Country Roads(prim算法)
I am going to my home. There are many cities and many bi-directional roads between them. The cities ...
- 【lightoj-1002】Country Roads(dijkstra变形)
light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...
- Light oj-1002 - Country Roads,迪杰斯特拉变形,不错不错~~
1002 - Co ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
随机推荐
- maven生命周期和依赖的范围
转载:http://blog.csdn.net/J080624/article/details/54692444 什么是依赖? 当 A.jar 包用到了 B.jar 包时,A就对B产生了依赖: 在项目 ...
- javascript闭包诡异的问题
var funcs = []; for (var i = 0; i < 3; i++) { // let's create 3 functions funcs[i] = function() { ...
- MySQL Study之--Percona Server版本号
MySQL Study之--Percona Server版本号 1.简单介绍 Percona 为 MySQL 数据库server进行了改进.在功能和性能上较 MySQL 有着非常显著的提升. ...
- Leetcode--easy系列10
#205 Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...
- Qt移动应用开发(四):应用粒子特效
Qt移动应用开发(四):应用粒子特效 上一篇文章介绍了Qt Quick是如何对帧动画进行支持的.帧动画的实现离不开状态机.而状态机.动画和状态切换(transitions)则是Qt框架的核心内容.也就 ...
- 无限级分类Asp.net Mvc实现
无限级分类Asp.net Mvc实现 无限级分类涉及到异步加载子类.加载当前类和匹配问题,现在做一个通用的实现. (一) 效果如下: (二)设计.实现及使用 (1)数据库 (a)表设计db ...
- java 生成压测数据
询价接口压测,需要批量生成数据, 数据包括4个字段(车牌号,车架号,发动机号,支付号)licenseNo,vehicleFrameNo,engineNo,payFlowId 需符合LoadRunner ...
- MyEclipse搭建SSH(Struts2+Spring2+Hibernate3)框架项目教程
对Struts.spring.hibernate大体上了解一遍后,就是针对这个几个框架的整合了. 怎样整合,请看以下: 第一:Struts2的jar和xml配置文件: jar包: commons-fi ...
- 怎样在 AutoLayout 中使用 UIScrollView (多个ContentView)
http://codehappily.wordpress.com/2013/11/14/ios-how-to-use-uiscrollview-with-autolayout-pure-autolay ...
- IDEA-Maven的环境配置及使用
一.Maven的下载 IDEA的往期下载地址:https://www.jetbrains.com/ 1.点击进入 1.往期的下载地址:http://www.apache.org/ 操作步骤:我们点击进 ...