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统计桥边个数即可. 否则提取出$[ ...
随机推荐
- 运行Google 官方zxing二维码扫描器
首先,要去下载Zxing的源码,由于Zxing 的服务内容比较广,我们先把所有的源码都下载下来,使用的时候根据需要加载. 或者从开源中国下载https://www.oschina.net/questi ...
- 【APEX初步】【2】【sObjects与数据库】
由于apex是与数据库集成的.我们可以直接用apex访问数据库.每条记录就是一个SObject对象
- Python自动化开发-简介
1.Python简介 Python创始人 Guido Van Rossum,人称"龟叔",1989年圣诞节期间,为了在阿姆斯特丹打发时间,开发的一个新的脚本解释程序 作为ABC语 ...
- GourdScan & sqlmapapi
0x01 Windows下配置GourdScan 0x0101 GourdScan项目地址:https://github.com/code-scan/GourdScan PHP环境 + ...
- Haskell Seq函数和严格计算
参考链接http://stackoverflow.com/questions/11046590/the-seq-function-and-strictness https://wiki.haskell ...
- 防止sql注入 查询的sql
and userName like concat('%',#{userName},'%')
- Nuget 学习二
打包自己的类库 准备工作: 1)nuget 账号: https://www.nuget.org/ 2)nuget 包管理器 点击下载:NuGetPackageExplorer,安装完应该是酱紫. 开始 ...
- php的redis函数
phpredis中文手册--<redis中文手册> php版 redis中文手册:http://readthedocs.org/docs/redis/en/latest/ 本文是参考& ...
- java读写锁源码分析(ReentrantReadWriteLock)
读锁的调用,最终委派给其内部类 Sync extends AbstractQueuedSynchronizer /** * 获取读锁,如果写锁不是由其他线程持有,则获取并立即返回: * 如果写锁被其他 ...
- css基础和心得(一)
css开始 p{ font-size:12px; color:red; font-weight:blod } p为选择符p{}又称选择器,指明网页中要应用样式规则的元素.属性和值之 ...