HDU 1596 最短路变形
这道题怎么都是TLE,报警了,先放在这
http://acm.hdu.edu.cn/showproblem.php?pid=1596
#include <iostream>
#include <iomanip>
using namespace std; #define MAXVEX 1001 double matrix[MAXVEX][MAXVEX];
double D[MAXVEX]; int n; void Dijkstral(int v0)
{
int v,w,k=;
double max;
int final[MAXVEX]; for(int i = ;i<=n;i++)
{
D[i] = matrix[v0][i];
final[i] = ;
} final[v0] = ; for(v=;v<=n;v++)
{
max = -;
for(w=;w<=n;w++)
{
if(D[w]>max && !final[w])
{
max = D[w];
k = w;
}
} final[k] = ; for(w=;w<=n;w++)
{
if(max*matrix[k][w]>D[w] && !final[w])
{
D[w]=max*matrix[v][w];
}
}
} } int main()
{
while(cin>>n)
{
for(int i = ;i<=n;i++)
{
for(int j = ;j<=n;j++)
cin>>matrix[i][j];
} int m;
cin>>m;
while(m--)
{
int a,b;
cin>>a>>b; Dijkstral(a); if(D[b])
cout<<fixed<<setprecision()<<D[b]<<endl;
else
cout<<"What a pity!\n";
}
}
return ;
}
ac了,1482MS
#include <cstdio>
using namespace std;
const int L = ;
const int INF = <<; double map[L][L];
int vis[L];
double dis[L]; int n,m; void Dijkstra(int s)
{
int i,j,k;
double max;
for(i = ;i<=n;i++)
{
dis[i] = map[s][i];
vis[i] = ;
} vis[s] = ;
dis[s] = ; for(i = ;i<=n;i++)
{
max = ;
for(j = ;j<=n;j++)
{
if(dis[j]>max && !vis[j])
{
k = j;
max = dis[j];
}
} vis[k] = ; for(j = ;j<=n;j++)
{
if(dis[j] < max*map[k][j] && !vis[j])
{
dis[j] = max*map[k][j];
}
}
}
} void init()
{
int i,j;
for(i = ;i<=n;i++)
{ for(j = ;j<=n;j++)
{
scanf("%lf",&map[i][j]);
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int r,t; init(); scanf("%d",&r);
while(r--)
{
int a,b;
scanf("%d%d",&a,&b);
Dijkstra(a);
if(dis[b])
printf("%.3lf\n",dis[b]);
else
printf("What a pity!\n");
} } return ;
}
HDU 1596 最短路变形的更多相关文章
- hdu 1596(Floyd 变形)
http://acm.hdu.edu.cn/showproblem.php?pid=1596 find the safest road Time Limit: 10000/5000 MS (Java/ ...
- hdu 3986(最短路变形好题)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- hdu 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDOJ find the safest road 1596【最短路变形】
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1596 find the safest road (变形SP && dij+heap)
Problem - 1596 变形最短路问题,给出邻接矩阵,要求求出给定点对间安全率最大值. 这题可以用dijkstra+heap来做.对于每一个查询,做一次dij即可. 代码如下: #include ...
- 最短路变形题目 HDU多校7
Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M sh ...
- HDU.1596 find the safest road (Floyd)
HDU.1596 find the safest road (Floyd) 题意分析 与普通的最短路不太相同,本题有些许的变化. 1. 要找到由i到j最安全的路,故在求解的时候要保证mp[i][j]尽 ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- iOS学习笔记(7)——解析json中的中文
NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"]; NSError *error = ni ...
- 【2】JMicro微服务-Hello World
如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 1. 首先完成 JMicro微服务-RPC体验 的1到5步. 按默认方式启动ZK及Redis: JDK需要Java8及以上. ...
- leetcode-482-License Key Formatting
题目描述: You are given a license key represented as a string S which consists only alphanumeric charact ...
- 华南理工大学“三七互娱杯”程序设计竞赛 HRY and codefire(概率期望DP)
https://ac.nowcoder.com/acm/contest/874/A 题目:有两个账号 , 一开始都为0级 , 求任意一个账号升级到N的期望 要求:如果当前账号嬴了 , 就继续沿用当前的 ...
- 认识CSS中高级技巧之用户界面样式
前端之HTML,CSS(九) CSS高级技巧 CSS用户界面样式 用户界面样式指更改用户操作样式,如更改用户的鼠标样式.表单轮廓等. 鼠标样式cursor cursor属性具有属性值有:default ...
- maven工程下testng简单使用
创建maven工程后,将Repository仓库中maven代码粘贴复制到pom.xml文件中,仓库地址:<!-- https://mvnrepository.com/artifact/org. ...
- java基本编译
1.java语言严格区分大小写:1个源文件其中最多只能有一个public类:源文件必须和public类同名:每个类对应一个class字节码,且同名. 2.编译. javac -d dir Hell ...
- CentOS7编译curl
1.下载curl源代码 https://curl.haxx.se/download.html 2.进入curl目录 ./configure --prefix=/usr/local/curl make ...
- 【Kafka源码】Kafka启动过程
一般来说,我们是通过命令来启动kafka,但是命令的本质还是调用代码中的main方法,所以,我们重点看下启动类Kafka.源码下下来之后,我们也可以通过直接运行Kafka.scala中的main方法( ...
- jQuery事件之传递参数
一.jQuery绑定事件的三种方法 我们这里首先复习一下jQuery绑定事件的三种方法: target.click(function(){}); target.on("click" ...