考试的T3,拿暴力+剪枝卡过去了.

没想到 CF 上也能过 ~

code:

#include <bits/stdc++.h>
#define N 100004
#define LL long long
#define inf 1000000000000000
using namespace std;
int go[N];
namespace IO
{
char *p1, *p2, buf[100000];
#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )
inline int rd() {
int x = 0;
char c = nc();
while (c < 48) {
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x;
}
inline void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
};
LL d[N];
vector<int>G[N];
int n,m,Q,done[N],inq[N];
struct edge
{
LL val;
int u,v;
}ee[N];
deque<int>q;
void dijkstra()
{
for(int i=1;i<=n;++i) d[i]=inf,inq[i]=0;
d[1]=0, inq[1]=1;
q.push_back(1);
while(!q.empty())
{
int u=q.front(); q.pop_front();
for(int i=G[u].size()-1;i>=0;--i)
{
edge e=ee[G[u][i]];
inq[e.u]=0;
if(d[e.v]>d[u]+e.val)
{
d[e.v]=d[u]+e.val;
if(!inq[e.v])
{
if(q.empty()||d[e.v]<d[q.front()]) q.push_front(e.v);
else q.push_back(e.v);
inq[e.v]=1;
}
}
}
}
}
int main()
{
// int t=clock();
// IO::setIO("input");
int i,j,flag=1;
n=IO::rd();
m=IO::rd();
Q=IO::rd();
// scanf("%d%d%d",&n,&m,&Q);
for(i=1;i<=m;++i)
{
ee[i].u=IO::rd();
ee[i].v=IO::rd();
ee[i].val=IO::rd();
G[ee[i].u].push_back(i);
}
dijkstra();
for(i=1;i<=n;++i) if(d[i]==inf) go[i]=0; else go[i]=1;
for(i=1;i<=Q;++i)
{
int opt,pp;
opt=IO::rd();
pp=IO::rd();
// scanf("%d%d",&opt,&pp);
if(opt==2)
{
for(j=1;j<=pp;++j)
{
int x=IO::rd();
++ee[x].val;
}
flag=0;
}
else
{
if(!go[pp])
{
printf("-1\n");
}
else
{
if(!flag)
{
flag=1;
dijkstra();
}
printf("%lld\n",d[pp]);
}
}
}
// printf("%d\n",clock()-t);
return 0;
}

  

CF843D Dynamic Shortest Path spfa+剪枝的更多相关文章

  1. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

  2. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  3. cf 843 D Dynamic Shortest Path [最短路+bfs]

    题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...

  4. Dynamic Shortest Path CodeForces - 843D (动态最短路)

    大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...

  5. ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  6. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  7. SPFA(Shortest Path Faster Algorithm)

    特别说明 本文转载自三金(frinemore)的博客: 点这 前言 1.关于SPFA,它没死. 2.接下来的所有代码,都是自己手写的(未检查正确性,补充的代码有检查过,是对的),有错误请帮忙指出. S ...

  8. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  9. Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm

    A method is presented for finding a shortest path from a starting place to a destination place in a ...

随机推荐

  1. Python习题001

    作业1 * 用条件语句写一个BMI(体重除以身高的平方)指数* 低于18.5:过轻* 18.5 - 25 正常* 25 - 28 过重* 28 - 32 肥胖* 高于32 严重肥胖 weight = ...

  2. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  3. C++语言类之间的关系

    在c++中通过类定义对象,而类与类之间也有着复杂的关系,所以题外话,我能理解到c++的编写者可能就想通过计算机语言去模拟世界万物之间的关系,这篇帖子主要从横向和纵向去讨论类之间关系 而在一个类中想要使 ...

  4. Maven学习存档(2)——settings.xml配置

    二.settings.xml配置 2.1 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Lic ...

  5. S04_CH02_工程移植ubuntu并一键制作启动盘

    S04_CH02_工程移植ubuntu并一键制作启动盘 2.1概述 2.2搭建硬件系统 本章硬件工程还是使用<S04_CH01_搭建工程移植LINUX/测试EMMC/VGA>所搭建的VIV ...

  6. Android去评分,分享

    去评分: 跳到手机中已安装的市场评分页面 Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent ...

  7. php-获取某个文件夹下面的文件数量

    /** * 获取文件夹下文件的数量 * @param $url 传入一个url如:/apps/web * @return int 返回文件数量 */ public function getFileNu ...

  8. win7用驱动精灵安装了bcm94352ac蓝牙驱动后还是不能用蓝牙的解决方法

    驱动精灵安装了驱动后,设备管理器处显示Bluetooth USB,但是没法用蓝牙,找不到蓝牙图标,后来在华硕官方下载了win7的Broadcom 蓝牙驱动程序装上之后就好了

  9. Windows cmd操作文件夹

    ir // 列出目录下所有文件夹 rd dirname // 删除dirname文件夹(空文件夹) rd /s/q dirname // 删除dirname文件夹(非空)

  10. Feign的理解

    Feign是什么? Feign是一个http请求调用的轻量级框架,也可以说是声明式WebService客户端 Feign的作用 可以以Java接口注解的方式调用Http请求,它使java调用Http请 ...