T3 最短路 line

【问题描述】

给定一个 n 个点,m 条边的有向图,每个点有一个权值 a[i],表示这个点要到达多少次,1 为起始点,从 1 到 i 的距离为 d[i],请你输出∑a[i]*d[i],如果图不连通请输出“No Answer”。

【输入格式】

第一行为 n,m,含义见描述
接下来一行包含 n 个数,即 a[i]
接下来 m 行,每行三个数表示一条边的起点,终点,以及权值

【输出格式】

No Answer 或者一个数字

【样例输入】

3 2

1 1 2

1 2 15

2 3 1

【样例输出】

47

【数据说明】
n<=50000,m<=100000
∑w[i]、∑c[i]不超过 maxlongint ,可能存在重边

Solution

最短路板子题,算出1到每个点的最短路d[i]×a[i]

Code

// <line.cpp> - Thu Oct  6 08:17:54 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#define MOD 1000000007
#define INF 1e9
#define IN inline
#define RG register
using namespace std;
typedef long long LL;
typedef long double LB;
const int MAXN=;
const int MAXM=;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
register int w=,q=;register char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
}
struct Dijskra{
static const int N=,M=;
int n,m,t;int d[N],fr[N],a[N];int to[M],ne[M],W[M];bool u[N];
struct node{
int s,p;
bool operator<(node a)const{return s>a.s;}
};
priority_queue<node>q;
void link(int u,int v,int w){
to[++t]=v;ne[t]=fr[u];fr[u]=t;W[t]=w;
}
void read(){
n=gi(),m=gi();
for(int i=;i<=n;i++)a[i]=gi();
while(m--){
int u=gi(),v=gi(),w=gi();
link(u,v,w);
}
}
void Dij(int begin){
for(int i=;i<=n;i++)d[i]=INF;
q.push((node){d[begin]=,begin});memset(u,,sizeof(u));
while(!q.empty()){
while(u[q.top().p]&&!q.empty())q.pop();
if(q.empty())break;
int x=q.top().p;q.pop();u[x]=;
for(int o=fr[x],y;y=to[o],o;o=ne[o])
if(d[x]+W[o]<d[y]){
d[y]=d[x]+W[o];
q.push((node){d[y],y});
}
}
for(int i=;i<=n;i++)
if(d[i]==INF){printf("No Answer");exit();}
LL ans=;
for(int i=;i<=n;i++)ans+=1LL*d[i]*a[i];
printf("%lld",ans);
}
}e;
int main()
{
freopen("line.in","r",stdin);
freopen("line.out","w",stdout);
e.read();e.Dij();
return ;
}

T3 最短路 line的更多相关文章

  1. 10.6 Graph Test

    一套图论的练习题,各个方面都有挺好的 第一第二题有一定难度(来源POI),第三第四题比较水 但我并没考好 T1 特工 szp T2 洞穴 zaw T3 最短路 line T4 最小差异值 dvalue

  2. ABAP 内表 详解

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. bzoj4306: 玩具厂

    Description 在JIH考察的地图中有N个城市,被公路依次连成了一个环,JIH想在这些城市中建一个玩具厂.城市和公路都被编号为1..N,i号公路连接i-1号城市与i号城市(1号公路连接N号城市 ...

  4. NOIp2017 滚粗记

    NOIp2017 滚粗记 Day0 早上 早自习的时候,班主任忽然告诉我们, 我们要参加期中考试... 这对于我们真是一个沉重的打击... 但是,管不着了 明天就死去考试了 上午 \(8:10\)到了 ...

  5. [日常] NOIP 2017滚粗记

    突然挑了这么个滑稽的时间补了游记... (成绩日常延时再加上人太菜估计基本上就是颓废记录) 然而文化课太废可能会被强制退役QAQ所以先补了再说吧 day0 一大早被老姚交代了个开十一机房门的任务... ...

  6. 2017.08.06【NOIP提高组】模拟赛B组

    Summary 今天的比赛60+100+100=260分,没有想到第一题正解是搜索,我与AK差一段距离,这段距离,叫倒着搜.总的来说不是很难. Problem T1 天平 题目大意 给你N个排序好的砝 ...

  7. gdb 调试及优化

    调试程序时,在gdb内p var,会提示 No symbol "var" in current context. 即使没有使用任何编译优化选项,仍然不能查看,可能是这些变量被优化到 ...

  8. 2018.10.17NOIP模拟赛解题报告

    心路历程 预计得分:\(100 + 100 +100\) 实际得分:\(100 + 100 + 60\) 辣鸡模拟赛.. 5min切掉T1,看了一下T2 T3,感觉T3会被艹爆因为太原了.. 淦了20 ...

  9. 转:Oracle:删除表空间

    原文:http://space.itpub.net/40239/viewspace-365948 OMF和非OMF管理的数据文件在DROP TABLESPACE时是否会自动删除,做了测试: SQL&g ...

随机推荐

  1. spring Boot 不认Mapper.xml

    很久以前的笔记了,大约就是用Generatro工具自动生成代码的时候,springboot找不到mapper.xml 之前,由于用mybatis-generator自动生成了entity,dao,ma ...

  2. http主要请求头

    一.内容协商 1.Accept:希望服务器返回的数据格式,如下面的:text/javascript, application/javascript, application/ecmascript, a ...

  3. ThinkPHP5.X PHP5.6.27-nts + Apache 通过 URL 重写来隐藏入口文件 index.php

    我们先来看看官方手册给出关于「URL 重写」的参考: 可以通过 URL 重写隐藏应用的入口文件 index.php ,Apache 的配置参考: 1.http.conf 配置文件加载 mod_rewr ...

  4. Python之目录结构

    Python之目录结构 项目名project_name project_name -|--bin (可执行文件) --|--start.py import os,sys #设置环境变量 BASE_DI ...

  5. LeetCode(30) Substring with Concatenation of All Words

    题目 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  6. LeetCode(6) ZigZag Conversion

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. hdu 4948 Kingdom(推论)

    hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...

  8. Python模块:Re模块、附软件开发目录规范

    Re模块:(正则表达式) 正则表达式就是字符串的匹配规则 正则表达式在多数编程语言里都有相应的支持,Python里面对应的模块时re 常用的表达式规则:(都需要记住) “ . ”   #  默认匹配除 ...

  9. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

  10. Ubuntu 16.04修改显示字体大小(包括GNOME/Unity)

    在Ubuntu中字体都基本偏大,且和分辨率无关. Unity: 安装Unity Tweak Tool sudo apt-get install unity-tweak-tool GNOME: 打开优化 ...