hdu 3986 Harry Potter and the Final Battle
一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA。。。。。。
思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边。所以枚举一下最短路上的边,每次求最短路即可。
#include<stdio.h>
#include<vector>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = + ;
const int INF = 0x7FFFFFFF;
int n, m, anss;
vector<int>ljb[maxn];
vector<int>bbb[maxn][maxn];
int jz[maxn][maxn];
int cost[ + ], flag[ + ], ff[maxn], dist[maxn];
int s[maxn], path[maxn]; void SPFA()
{
int i, ii;
queue<int>Q;
memset(ff, , sizeof(ff));
for (i = ; i <= n; i++) dist[i] = INF, s[i] = -;
ff[] = ; Q.push(); dist[] = ; s[] = ;
while (!Q.empty())
{
int h = Q.front(); Q.pop(); ff[h] = ;
for (i = ; i < ljb[h].size(); i++)
{
for (ii = ; ii < bbb[h][ljb[h][i]].size(); ii++)
{
if (flag[bbb[h][ljb[h][i]][ii]] ==)
{
if (dist[h] + cost[bbb[h][ljb[h][i]][ii]] < dist[ljb[h][i]])
{
dist[ljb[h][i]] = dist[h] + cost[bbb[h][ljb[h][i]][ii]];
s[ljb[h][i]] = h;
if (ff[ljb[h][i]] == )
{
ff[ljb[h][i]] = ;
Q.push(ljb[h][i]);
}
}
}
}
}
}
} int main()
{
int sb;
scanf("%d", &sb);
while (sb--)
{
int i, j, u, v;
scanf("%d%d", &n, &m);
memset(flag, , sizeof(flag));
for (i = ; i <= n; i++) for (j = ; j <= n; j++) jz[i][j] = INF;
for (i = ; i <= n; i++) for (j = ; j <= n; j++) bbb[i][j].clear();
for (i = ; i <= n; i++) ljb[i].clear();
for (i = ; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &cost[i]);
if (jz[u][v] == INF) jz[u][v] = i, jz[v][u] = i;
if (cost[i] < cost[jz[u][v]]) jz[u][v] = i, jz[v][u] = i;
bbb[u][v].push_back(i);
bbb[v][u].push_back(i);
ljb[u].push_back(v);
ljb[v].push_back(u);
}
SPFA();
anss = -;
if (dist[n] != INF)
{
path[] = n; int q = ;
while ()
{
path[q] = s[path[q - ]];
if (path[q] == ) break;
q++;
}
for (i = ; i <= q - ; i++)
{
flag[jz[path[i]][path[i + ]]] = ;
SPFA();
if (dist[n] == INF){ anss = -; break; }
if (dist[n] > anss) anss = dist[n];
flag[jz[path[i]][path[i + ]]] = ;
}
}
printf("%d\n", anss);
}
return ;
}
hdu 3986 Harry Potter and the Final Battle的更多相关文章
- hdu 3986 Harry Potter and the Final Battle (最短路径)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- 【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle
http://acm.hdu.edu.cn/showproblem.php?pid=3986 [题意] 给定一个有重边的无向图,T=20,n<=1000,m<=5000 删去一条边,使得1 ...
- hdu3986Harry Potter and the Final Battle
给你一个无向图,然后找出当中的最短路, 除去最短路中的随意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删 ...
- hdu 3986(最短路变形好题)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)
Harry Potter and the Hide Story Problem Description iSea is tired of writing the story of Harry Pott ...
- HDU 3986
http://acm.hdu.edu.cn/showproblem.php?pid=3986 从开始的最短路里依次删一条边,求新的最短路,求最长的最短路 删边操作要标记节点以及节点对应的边 #incl ...
- HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)
Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...
- Bridges: The Final Battle
对修改操作按时间分治,设$solve(l,r,n,m)$为考虑时间在$[l,r]$的修改操作,作用范围是$n$个点,$m$条边的图. 若$l=r$,则暴力Tarjan统计桥边个数即可. 否则提取出$[ ...
随机推荐
- 分享下mac安装xamarin跨平台开发环境的坑
之前在vs2015上安装好了xamarin环境,考虑到调试IOS仍然需要mac机,昨天决定直接在mac上安装xamarin. 安装完所有的效果如上图,此时已经可以创建安卓和IOS环境. 我安装过程中, ...
- “System.FormatException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 其他信息: 该字符串未被识别为有效的 DateTime。
前台用过jquery ajax将值传递到 后台的一般处理程序 并且报了这个异常 原因是:前台传递过来的时间格式不对 需要把 "/"换成 "-" 例如:20 ...
- oracle——用户
新增用户 用system用户登录 CREATE USER TEST1 IDENTIFIED BY TEST1; CREATE USER:创建用户命令,后跟用户名: IDENTIFIED BY:后跟密码 ...
- gridcontrol datatemplate trigger
<TextBlock Name="textBlock" HorizontalAlignment="Left" Text="{Binding Va ...
- spring boot maven 插件
spirng boot 需要使用专用maven插件打包,才能打包.引入如下. <build> <plugins> <plugin> ...
- 查增删改MySQL数据库固定模式
省略相关包的导入... public class Base { public static Connection connection = null; public static PreparedSt ...
- Vultr日本vps搭建ss/ssr/openvpn免流教程
每个月的手机流量不够用,运营商流量套餐价格偏高,怎么才能省钱?你在淘宝上,搜索手机免流,可找到很多奸商销售免流套餐,一块钱可买1GB流量,免流原理是什么?自己能搞吗? 手机免流原理 手机运营商中国电信 ...
- FileOutputStream flush()
FileOutputStream 继承 OutputStream ,flush方法查看源码方法体为空,所以flush没起到清除缓存的作用 改用BufferedOutputStream再调用flush( ...
- metrics实践 (metrics-spring)
这里主要介绍metrics与spring集成的使用方式. 1 添加maven依赖 <dependency> <groupId>com.ryantenney.metrics&l ...
- mac安装lavaral
1,安装composer # brew install josegonzalez/php/composer