hdu3986 spfa + 枚举最短路上的边
题意:
删除一条边后,求最短路中最长的那个(敌人搞破坏).
思路:
如果你是敌人你肯定删除最短路上的边,删除别的边最短路的值是不会变的,所以直接枚举最短路上的边去删除,取得最大的就行了...
#include<stdio.h>
#include<string.h>
#include<queue>
#define N_node 1005
#define N_eage 110000
#define inf 1000000000
using namespace std;
typedef struct
{
int from ,to ,next ,cost;
}STAR;
STAR E[N_eage];
int list[N_node] ,tot;
int mer[N_eage] ,s_x[N_node];
void add(int a ,int b ,int c)
{
E[++tot].from = a;
E[tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
}
void spfa(int s ,int n ,int key)
{
int mark_q[N_node] = {0};
mark_q[s] = 1;
for(int i = 0 ;i <= n ;i ++)
s_x[i] = inf;
s_x[s] = 0;
queue<int>q;
q.push(s);
if(key == -1)
memset(mer ,255 ,sizeof(mer));
while(!q.empty())
{
int xin ,tou;
tou = q.front();
q.pop();
mark_q[tou] = 0;
for(int k = list[tou] ;k ;k = E[k].next)
{
if(k == key) continue;
xin = E[k].to;
if(s_x[xin] > s_x[tou] + E[k].cost)
{
s_x[xin] = s_x[tou] + E[k].cost;
if(key == -1) mer[xin] = k;
if(!mark_q[xin])
{
mark_q[xin] = 1;
q.push(xin);
}
}
}
}
return ;
}
int main ()
{
int t ,i ,n ,m ,a ,b ,c;
scanf("%d" ,&t);
while(t--)
{
scanf("%d%d" ,&n ,&m);
memset(list ,0 ,sizeof(list));
tot = 1;
while(m--)
{
scanf("%d %d %d" ,&a ,&b ,&c);
add(a ,b ,c);
add(b ,a ,c);
}
int ans = -1;
spfa(1 ,n ,-1);
int kg = 0;
for(i = mer[n] ;i + 1 ;i = mer[E[i].from])
{
spfa(1 ,n ,i);
if(s_x[n] == inf)
{
kg = 1;
break; /////*********如果有一个边删除后不连通了,那么敌人肯定毁灭着一条.
}
if(ans < s_x[n] )
ans = s_x[n];
}
if(kg) ans = -1;
printf("%d\n" ,ans);
}
return 0;
}
hdu3986 spfa + 枚举最短路上的边的更多相关文章
- hdu3986 spfa+枚举
这题让我第一次感受到了什么叫做在绝望中A题.这题我总共交了18次,TLE不知道几次,WA也不知道几次. 这题不能用dijkstra,用这个我一直超时(我没试过dij+优先队列优化,好像优先队列优化后可 ...
- Crowd Control(输出不在最大值最小化的最短路上的边)
题意: 就是求完最大值最小化 然后输出在这条最大值最小化的最短路上的点的不在最短路上的边,emm.... 解析: 很明显,先套spfa最大值最小化模板,emm... 在更新d的时候 用一个pre去记 ...
- VIJOS-P1446 最短路上的统计
JDOJ 1523: VIJOS-P1446 最短路上的统计 JDOJ传送门 Description 一个无向图上,没有自环,所有边的权值均为1,对于一个点对(a,b),我们要把所有a与b之间所有最短 ...
- find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)
题目链接:http://codeforces.com/problemset/problem/144/D 思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要 ...
- BZOJ-1880 Elaxia的路线 SPFA+枚举
1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 921 Solved: 354 [Submit][Sta ...
- POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- CodeForces 666B World Tour(spfa+枚举)
B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input ...
- POJ1125 Stockbroker Grapevine(spfa枚举)
Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a me ...
随机推荐
- apicloud编译所需的ios证书的获取方法
在我们通过apicloud或hbuilderX这些工具打包ios应用的时候,需要一个ios证书. 那么我们如何生成这个ios证书呢?网上介绍的方法都是需要使用mac电脑,然后用mac电脑的钥匙串访问的 ...
- 为什么要用Spring Boot?
什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程,该框架使用了特定的方式来进行配置,从而 ...
- 如何在 ASP.NET Core 中写出更干净的 Controller
你可以遵循一些最佳实践来写出更干净的 Controller,一般我们称这种方法写出来的 Controller 为瘦Controller,瘦 Controller 的好处在于拥有更少的代码,更加单一的职 ...
- EF Core中通过Fluent API完成对表的配置
EF Core中通过Fluent API完成对表的配置 设置实体在数据库中的表名 通过ToTable可以为数据模型在数据库中自定义表名,如果不配置,则表名为模型名的复数形式 public class ...
- flutter资料
Flutter社区和资源传送门 新: 慕课网<Flutter入门与案例实战> | 中文网<Flutter实战>电子书 字体图标生成 http://fluttericon ...
- 给Winform中的TabControl添加更现代的拖拽功能
上周接到一个开发任务,大致是允许APP中的Tab拖动以成为一个独立Tab,脱离之前的TabControl,就是现在Web拖动标签页创建新窗口的功能,现在浏览器必备的功能,应该很简单,然而我司采用的Do ...
- SpringBoot自动配置探究
@SpringBootApplication @SpringBootApplication表示SpringBoot应用,标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就 ...
- python-给一个参数n,例如3:先输出1,2,3,4,5,6,7,8,9,每三个数后换行,后输出1,4,7,2,5,8,3,6,9
""" 2 定义一个函数,fn(n)其中n表示输入n行n列的矩阵,需要满足的要求是在n为 3时先输出 3 1 2 3 4 4 5 6 5 7 8 9 6 后输出 7 1 ...
- EntityFrameworkCore之工作单元的封装
1. 简介 2. DbContext 生命周期和使用规范 2.1. 生命周期 2.2. 使用规范 2.3. 避免 DbContext 线程处理问题 3. 封装-工作单元 3.1. 分析 3.2. 设计 ...
- 在C#中使用 CancellationToken 处理异步任务
在 .NET Core 中使用异步编程已经很普遍了, 你在项目中随处可见 async 和 await,它简化了异步操作,允许开发人员,使用同步的方式编写异步代码,你会发现在大部分的异步方法中,都提供了 ...