hdu 3631 Shortest Path
floyd算法好像很奇妙的样子。可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = ;
const int INF = 0x7FFFFFFF;
int A[maxn][maxn], flag[maxn];
int n, m, q; void floyd(int x)
{
int i, j;
for (i = ; i < n; i++)
{
for (j = ; j < n; j++)
{
if (i == x || j == x) continue;
if (A[i][x] == INF || A[x][j] == INF) continue;
if (A[i][x] + A[x][j] < A[i][j]) A[i][j] = A[i][x] + A[x][j];
}
}
} int main()
{
int i, j, u, v, c, koko = ;
while (~scanf("%d%d%d", &n, &m, &q))
{
if (n == && m == && q == ) break;
memset(flag, , sizeof(flag));
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (i == j) A[i][j] = ;
else A[i][j] = INF;
}
}
for (i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &c);
if (c < A[u][v]) A[u][v] = c;
}
int oo, pp, qq;
if (koko != ) printf("\n");
printf("Case %d:\n", koko++);
for (i = ; i < q; i++)
{
scanf("%d", &oo);
if (oo == )
{
scanf("%d", &pp);
if (flag[pp] == ) printf("ERROR! At point %d\n", pp);
else if (flag[pp] == ){ flag[pp] = ; floyd(pp); }
}
else if (oo == )
{
scanf("%d%d", &pp, &qq);
if (flag[pp] == || flag[qq] == )printf("ERROR! At path %d to %d\n", pp, qq);
else
{
if (A[pp][qq] == INF) printf("No such path\n");
else printf("%d\n", A[pp][qq]);
}
}
}
}
return ;
}
hdu 3631 Shortest Path的更多相关文章
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- HDU 5636 Shortest Path 暴力
Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
- HDU - 4725_The Shortest Path in Nya Graph
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 5636 Shortest Path
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...
- HDU 5636 Shortest Path 分治+搜索剪枝
题意:bc round 74 分析(官方题解): 你可以选择分类讨论, 但是估计可能会写漏一些地方. 只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^ ...
- HDU 5636 Shortest Path(Floyd)
题目链接 HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...
- HDU 4479 Shortest path 带限制最短路
题意:给定一个图,求从1到N的递增边权的最短路. 解法:类似于bellman-ford思想,将所有的边先按照权值排一个序,然后依次将边加入进去更新,每条边只更新一次,为了保证得到的路径是边权递增的,每 ...
随机推荐
- C语言之循环结构 for(二)
一 for循环的嵌套使用 for循环的嵌套语法: for(int i =0;i<10;i++){ for(int j=0;j<5;j++){ } } 一般用来打印平面,或者控制平面,或者说 ...
- .NET 基础 一步步 一幕幕[面向对象之堆、栈、引用类型、值类型]
堆.栈.引用类型.值类型 内存分为堆和栈(PS:还有一种是静态存储区域 [内存分为这三种]),值类型的数据存储在栈中,引用类型的数据存储在堆中. 堆.栈: 堆和栈的区别: 栈是编译期间就分配好的内存空 ...
- Idea高级用法
一.快速打开Action: Ctrl+Shift+A 列表中会列出所有的action,对应于idea的各种操作,例如: 输入backgroud,可以为编辑器设置背景 输入restclient,可以打开 ...
- iOS BUG的解决
当iOS程序报这样的错误 ld:261 dulicate symbols for architecture x86_64 clang:error:linker command failed with ...
- http缓存与cdn相关技术
阅读目录 一 http缓存 二.Http缓存概念解析 三.cdn相关技术 摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料, ...
- Object转换为字符并去空格
<div id="txt" style="display:none">1."不积跬步,无以至千里"的古语说明( A ) A.没有 ...
- [Q]打印机页边距设置
问题描述:当您在使用CAD批量打图精灵默认设置打印图纸(使用pdfFactory虚拟打印机),可能会发现打印出的图纸页边距比您手工打印(使用Adobe或系统打印机)的要偏大. Adobe虚拟打印机打印 ...
- case a.ass_term_unit when '01' then (case a.ass_profit_mode when '0' then round(sum(a.ass_amount*a.ass_annual_rate/365*365*a.ass_term/100) ,2) when '1' then round(sum(a.ass_amount*a.ass_annual_rate/
--01 年 02 月 03 日 select a.ass_due_date, case a.ass_term_unit when '01' then (case a.ass_profit_mode ...
- DevExpress的SpinEdit控件无法输入数字的问题
今天在发布程序后突然发现了这个问题,刚开始很莫名其妙的,因为在调试时从来没碰到过.然后经过测试发现,这个问题的原因和输入法有很大关系: 当你的输入法是中文状态时,是无法向框中输入数字的,此时只能点击上 ...
- "malloc: * error for object 0x17415d0c0: Invalid pointer dequeued from free list * set a breakpoint in malloc_error_break to debug";
I've fixed this error with Xcode 8 on iOS 8.3. I've just changed Deployment Target from 8.3 to 8.0. ...