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. mybatis保存时将数据库自动生成的主键返回

    场景 保存订单数据和订单详情数据时需要将订单的主键作为关联子段添加到明细表中,需要将保存订单时的主键返回给供保存明细表时使用 添加xml中新增数据时的配置 <insert id="in ...

  2. LNMP构架搭建论坛

    1 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel fontconfig fo ...

  3. xtrbackup备份mysql

    mysqldump备份方式是采用逻辑备份,但是它最大的缺陷就是备份和恢复速度慢对于一个小于50G的数据库而言,这个速度还是能接受的,但如果数据库非常大,那再使用mysqldump备份就不太适合了. x ...

  4. buf.equals()

    buf.equals(otherBuffer) otherBuffer {Buffer} 返回:{Boolean} 返回一个 boolean 标识,无论 this 和 otherBuffer 是否具有 ...

  5. Final Battle #1 K题 Fire game

    Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...

  6. 第七章习题G题

    题意 给出如图案例,要你从某一点开始走,一直走到极限(即无法再进行扩展),这时你走过的点会连成一个数,不同的走法当然会有不同的数,要求是输出最大的数(注意每个方块走过一次就不能再走) 思路 •1.枚举 ...

  7. Archive log restore using RMAN for Logminer (http://www.dba-village.com/village/dvp_forum.OpenThread?ThreadIdA=26816)

    Subject: Archive log restore using RMAN for Logminer Author: Edwin Weele van der, Netherlands Date: ...

  8. org.osgi.framework.BundleException: Exception in org.eclipse.core.resources.ResourcesPlugin.start()

    http://freestyle21.cn 不知什么时候,启动eclipse的时候就一直不行,说是an error ..我查了下log 报错:org.osgi.framework.BundleExce ...

  9. maven的安装与环境变量配置

    1.下载maven 地址:http://maven.apache.org/download.cgi 点击下载 apache-maven-3.2.1-bin.zip. 2.安装配置,假设maven 解压 ...

  10. mysql排序关于英文字母abcd..xyz排序。

    mysql会自动进行比如pxj,pyj. 再根据p进行排序时候,会自动进行第二个字母的排序 select * from tbl_actor where first_char like 'p%' ord ...