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 ...
随机推荐
- dnsmasq possible DNS-rebind attack detected错误
最近在做openwrt的平台,dns使用的是dnsmasq,但是通过板子上网,将PC的dns设置成板子的时候,发现百度等都可以,但是公司邮箱打不开.公司邮箱的域名 xx-xx-notes.xxx.co ...
- Cubieboard搭建Hadoop指南
1.刷固件 出厂的Cubieboard是Android系统,需要Linux系统安装Hadoop,到此网址下载: http://dl.cubieboard.org/software/a20-cubiet ...
- iOS设计模式 - (1)概述
近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...
- [NPM] npm check to update the dependencies
To update the dependencies in the project, we can run: npx npm-check -u
- wsdl2objc定制(一)namespace
1.问题抛出: 如今还是有非常多人使用 wsdl2objc 来调用webservice,可是有时候会有不开心的事情发生, <soap:Envelope xmlns:soap="http ...
- Android 通过ADB Wireless无线调试应用
使用数据线调试应用难免不方便,本篇博客介绍使用ADB Wireless工具.当手机和电脑处在同一网络下.实现无线调试应用. ADB Wireless可以让手机用无线来代替USB连接.而使用ADB工具的 ...
- anaconda中新rdkit安装
1. 执行 conda create -c rdkit -n my-rdkit-env rdkit 该步骤经测试发现需FQ,而模拟器无法完成FQ(至少我不知道方法), 因此在本机上配置好环境后复制粘贴 ...
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- Why was 80 Chosen as the Default HTTP Port and 443 as the Default HTTPS Port?
https://www.howtogeek.com/233383/why-was-80-chosen-as-the-default-http-port-and-443-as-the-default-h ...
- (21) java web的struts2框架的使用
在javaweb开发过程中,如果只使用servlet,jdbc,jsp进行开发,也可以遵从MVC的模式,这时候,servlet相当于control层,属于负责处理业务逻辑的控制器,同时也需要对获取和返 ...