大概做法是这样的

考虑最朴素的做法,预处理出1到所有点的最短路数组dis1和方案数数组cnt1,和预处理出n到所有点的最短路数组dis2和方案数数组出cnt2,然后暴力枚举点对(A,B),如果A和B之间没有连边,那么就可以考虑添加一条正权边,满足这个条件就能添加dis1[A]+dis2[B]+1<=dis1[n],且cnt1[A]*cnt2[B]>=X,由于是要使方案增加,所以将边权设为dis1[n]-(dis1[A]+dis2[B])是最好的,因为可以继承原有的最短路方案数。那么由于(A,B)和(B,A)只算一次,那么第一维枚举到A第二维枚举到B,和第一维枚举到B第二维枚举到A会不会算同一种呢,可以证明这种情况并不会出现重复技计数。

那么考虑优化,枚举点A,点B需满足dis1[A]+dis2[B]+1<=dis1[n],那么可以将dis2进行排序,然后二分出临界范围,然后查询出这个范围内cnt2[B]>=X/cnt[A]的数目,离线的话做法估计挺多的,我代码里用了主席树,最后还需要去掉范围内已经连边的点和自身。时间复杂度O(nlogn)

  代码

 #include<cstdio>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
using namespace std;
const int N = ;
const int M = ;
const int inf = ;
typedef pair<int,int> P;
priority_queue<P,vector<P>,greater<P> > Q;
int n,m,i,a,b,c,dis[N],cnt[N],dis1[N],cnt1[N],dis2[N],cnt2[N],X;
int id[N],ID[N],Id[N];
vector<P> e[N];
void gao(int x)
{
int i;
for (i=;i<=n;i++)
dis[i]=inf,cnt[i]=;
dis[x]=;
cnt[x]=;
Q.push(mp(,x));
while (!Q.empty())
{
P tmp=Q.top();
Q.pop();
x=tmp.sc;
if (dis[x]!=tmp.fi) continue;
for (i=;i<e[x].size();i++)
if (dis[x]+e[x][i].sc<dis[e[x][i].fi])
{
dis[e[x][i].fi]=dis[x]+e[x][i].sc;
cnt[e[x][i].fi]=cnt[x];
Q.push(mp(dis[e[x][i].fi],e[x][i].fi));
}
else
if (dis[x]+e[x][i].sc==dis[e[x][i].fi])
{
cnt[e[x][i].fi]+=cnt[x];
if (cnt[e[x][i].fi]>X) cnt[e[x][i].fi]=X;
}
}
} int ls[M],rs[M],s[M],tot,root[N];
void build(int &x,int a,int b)
{
x=++tot;
ls[x]=rs[x]=s[x]=;
if (b-a>)
{
int m=(a+b)>>;
build(ls[x],a,m);
build(rs[x],m,b);
}
}
void insert(int y,int &x,int a,int b,int l,int r)
{
x=++tot;
ls[x]=ls[y];rs[x]=rs[y];s[x]=s[y]+;
if ((a<=l)&&(r<=b))
return;
int m=(l+r)>>;
if (a<m) insert(ls[y],ls[x],a,b,l,m);
if (m<b) insert(rs[y],rs[x],a,b,m,r);
}
int query(int x,int a,int b,int l,int r)
{
if ((a<=l)&&(r<=b))
return s[x];
int m=(l+r)>>,ans=;
if (a<m) ans+=query(ls[x],a,b,l,m);
if (m<b) ans+=query(rs[x],a,b,m,r);
return ans;
}
bool cmp(int a,int b)
{
return dis2[a]<dis2[b];
}
bool CMP(int a,int b)
{
return cnt2[a]>cnt2[b];
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
if (n+m==) return ;
for (i=;i<=n;i++) e[i].clear();
scanf("%d",&X);
for (i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
e[a].pb(mp(b,c));
e[b].pb(mp(a,c));
}
gao();
for (i=;i<=n;i++)
dis1[i]=dis[i],cnt1[i]=cnt[i];
gao(n);
for (i=;i<=n;i++)
dis2[i]=dis[i],cnt2[i]=cnt[i]; tot=;
build(root[],,n);
for (i=;i<=n;i++)
id[i]=i;
sort(id+,id++n,cmp);
for (i=;i<=n;i++)
ID[id[i]]=i; for (i=;i<=n;i++)
Id[i]=i;
sort(Id+,Id++n,CMP);
for (i=;i<=n;i++)
insert(root[i-],root[i],ID[Id[i]]-,ID[Id[i]],,n);
long long ans=;
for (i=;i<=n;i++)
{
int l=,r=n;
while (l<=r)
{
m=(l+r)>>;
if (dis2[id[m]]+dis1[i]+<=dis1[n]) l=m+;else r=m-;
}
int j=r; l=;r=n;
while (l<=r)
{
m=(l+r)>>;
if (1LL*cnt1[i]*cnt2[Id[m]]>=X) l=m+;else r=m-;
} if (j) ans=ans+query(root[r],,j,,n); for (int k=;k<e[i].size();k++)
if (dis2[e[i][k].fi]++dis1[i]<=dis1[n])
if (1LL*cnt1[i]*cnt2[e[i][k].fi]>=X) ans--; if (dis1[i]+dis2[i]+<=dis1[N])
if (1LL*cnt1[i]*cnt2[i]>=X) ans--; }
printf("%lld\n",ans);
}
}

HDU5870 Alice's Adventure in Wonderland的更多相关文章

  1. the little schemer 笔记(10)

    第十章 What Is  the Value of All of This? entry条目 是由list表组成的 pair 对,pair 对的第一个list表是集合 set.另外,两个list表的长 ...

  2. Comparing the Performance of .NET Serializers(zz)

    The .NET framework comes with a variety of different serializers. Hopefully, my overview of these se ...

  3. zoj Treasure Hunt IV

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

  4. RSA实例破解

    Description: Decode the message. You intercept the following message, which you know has been encode ...

  5. ZOJ3629 Treasure Hunt IV(找到规律,按公式)

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

  6. python抓取数据构建词云

    1.词云图 词云图,也叫文字云,是对文本中出现频率较高的"关键词"予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨. 先看几个词 ...

  7. 生活英语读写MOOC-Literature Tutor-有声名著阅读推荐

    生活英语读写MOOC-Literature Tutor-有声名著阅读推荐 1. Alice's Adventures in Wonderland 爱丽丝漫游奇境记 音频与文本下载地址:链接:http: ...

  8. September 05th 2017 Week 36th Tuesday

    I always in the deepest despair, meet the most beautiful sunrise. 我总是在最深的绝望里遇见最美丽的惊喜. Some pessimist ...

  9. September 04th 2017 Week 36th Monday

    Try not to become a man of success but rather try to become a man of value. 不要努力去做一个成功的人,而要努力去做一个有价值 ...

随机推荐

  1. Service Locator 服务定位模式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. Spring和Struts2整合

    目的:spring容器管理Action类,代替Servlet 步骤:主要在配置文件 Struts2: 添加支持spring的jar包, 配置<action class="Action类 ...

  3. FK JavaScript之:ArcGIS JavaScript API之地图动画

    地图要素动画应用场景:动态显示地图上的要素的属性随着时间的改变而改变,并根据其属性的变化设置其渲染.比如:某水域项目中,随着时间的变化,动态展现水域的清淤进度 本文目的:对ArcGIS JavaScr ...

  4. bzoj3083 遥远的国度 && bzoj3626 LCA (树链剖分)

    今早刷了两道树剖的题目,用时两小时十五分钟= = 树剖的题目代码量普遍120+ 其实打熟练之后是很容易调的,不熟练的话代码量大可能会因为某些小细节调很久 3083:裸树剖+"换根" ...

  5. win7下装完ubuntu linux后,开机画面怎直接进入linux了,win7怎么启动

    修复 Windows 7 启动项重新启动ubuntu之后,结果发现选择windows 7的启动项,又进入到Ubuntu的安装界面.下面来说明如何修复,进人Ubuntu系统,打开“应用程序---附件-- ...

  6. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-2 工程及程序构架介绍

    视频简介: 该视频介绍iCore3应用开发平台出厂源代码中ARM部分程序构架. 源视频包下载地址: http://pan.baidu.com/s/1jHGnc7k   银杏科技优酷视频发布区: htt ...

  7. NV SDK 10 (1) Clipmaps

    Clipmaps sample: Abstract Clipmaps are a feature first implemented on SGI workstations that allow ma ...

  8. Nginx多个域名,https redirect to http

    背景描述:Nginx绑定多个域名,其中一个域名配置了https,如域名A:https://www.aaa.com:另外的域名B(http://www.bbb.com)没有配置SSL证书, 问题:以ht ...

  9. JAVA 对象调用理解图

  10. Asp.Net Mvc Areas 的用法与好处

    前言 在项目中为什么要使用Areas 进行分离 大家都知道,一般的Web应用都有前台(面向用户)和后台(面向管理员)两部分,我们希望以/localhost/Admin 开始的Url 是用户的后台管理地 ...