BZOJ3482 : [COCI2013]hiperprostor
对于每组询问,spfa求出f[i][j]表示从S出发,经过j条x边到达i的最短路。
若f[T][i]都为inf,则无解。
若f[T][0]为inf,则有无穷个解。
否则可以看作若干条直线,$O(n)$求出凸壳。
算出相邻两条直线交点横坐标并取下整,若刚好为整数则-1,设b[i]为i与i+1的交点的横坐标。
则第i(1<=i<t)段的贡献为首项为f(b[i-1]+1),末项为f(b[i])的等差数列。
最后一段的斜率为0,所以贡献为f[T][0]。
#include<cstdio>
typedef long long ll;
const int N=510,M=10010,inf=~0U>>1;
int n,m,i,j,x,y,z,S,T,g[N],v[M],w[M],nxt[M],ed,f[N][N],in[N][N],q[1000000][2];
int h,t,a[N],b[N],cnt;ll sum;
inline void read(int&a){
char c;
while(!((((c=getchar())>='0')&&(c<='9'))||(c=='x')));
if(c=='x'){a=0;return;}
a=c-'0';
while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';
}
inline void addedge(int x,int y,int z){v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;}
inline void add(int x,int y,int z){
if(y>=n)return;
if(f[x][y]<=z)return;
f[x][y]=z;
if(!in[x][y])in[x][y]=1,q[++t][0]=x,q[t][1]=y;
}
inline double cross(int x,int y){return (double)(f[T][x]-f[T][y])/(double)(y-x);}
inline int crossi(int x,int y){
int a=f[T][y]-f[T][x],b=x-y;
if(a<=0)return 0;
return(a-1)/b;
}
inline void cal(int k,int b,int l,int r){
if(l<1)l=1;
if(l>r)return;
cnt+=r-l+1;
sum+=((ll)(l+r)*k+2LL*b)*(r-l+1)/2;
}
int main(){
read(n),read(m);
while(m--)read(x),read(y),read(z),addedge(x,y,z);
read(m);
while(m--){
read(S),read(T);
for(i=1;i<=n;i++)for(j=0;j<n;j++)f[i][j]=inf;
h=1,t=0,add(S,0,0);
while(h<=t){
x=q[h][0],y=q[h++][1],in[x][y]=0;
for(i=g[x];i;i=nxt[i])add(v[i],y+(w[i]==0),f[x][y]+w[i]);
}
for(j=0;j<n;j++)if(f[T][j]<inf)break;
if(j==n){puts("0 0");continue;}
if(f[T][0]==inf){puts("inf");continue;}
for(t=0,i=n-1;~i;i--)if(f[T][i]<inf){
while(t>1&&cross(a[t-1],a[t])>=cross(a[t],i))t--;
a[++t]=i;
}
cnt=1,sum=f[T][0];
for(i=1;i<t;i++)cal(a[i],f[T][a[i]],b[i-1]+1,b[i]=crossi(a[i],a[i+1]));
printf("%d %lld\n",cnt,sum);
}
return 0;
}
BZOJ3482 : [COCI2013]hiperprostor的更多相关文章
- 【BZOJ 3482】 3482: [COCI2013]hiperprostor (dij+凸包)
3482: [COCI2013]hiperprostor Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 277 Solved: 81 Descrip ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- [COCI2013]DLAKAVAC
[COCI2013]DLAKAVAC 题目大意: 有一个长度为\(m(m\le1500)\)的\(01\)串\(A\),进行\(k(k\le10^{18})\)次操作.一次操作完的串中若\(A_i=1 ...
- 【JZOJ3238】【BZOJ3482】超空间旅行
description 在遥远的未来,行星之间的食品运输将依靠单向的贸易路线.每条路径直接连接两个行星,且其运输时间是已知的. 贸易商协会打算利用一项最近发现的新技术--超空间旅行,以增加一些新的航线 ...
随机推荐
- lvs之dr技术的学习与实践
1.配置测试环境 修改IP windows 200.168.10.4 lvs server ip:200.168.10.1 因为IP隧道模式只需要一个网卡 所以就停掉其他网卡 web server ...
- Linux Tomcat 简介
如今,基于Web的应用越来越多,传统的Html已经满足不了如今的需求.我们需要一个交互式的Web,于是便诞生了各种Web语言.如Asp,Jsp,Php等.当然,这些语言与传统的语言有着密切的联系,如P ...
- FOJ 1075
#include<stdio.h> #include<iostream> using namespace std; ]={,}; ]={}; void func() { ,fl ...
- js 实现图片预加载 (js操作 Image对象属性complete ,事件onload 异步加载图片)
通过js操纵DOM很多情况下都是为了实现和当前页html元素的异步载入,我谈谈对Image对象的一些认识.看个例子:<input type="button" name=&qu ...
- linux 多个文件中查找字符串
2015年2月9日 14:36:38 # find <directory> -type f -name "*.c" | xargs grep "<str ...
- Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法
在Android开发中,ListView有着很重要的地位,使用的场合也非常的多 错误提示:Your content must have a ListView whose id attribute is ...
- MongoDB副本集学习(二):基本测试与应用
简单副本集测试 这一节主要对上一节搭建的副本集做一些简单的测试. 我们首先进入primary节点(37017),并向test.test集合里插入10W条数据: . rs0:PRIMARY> ;i ...
- Java for LeetCode 149 Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Java for LeetCode 049 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 【HTTP协议】响应头中的Content-Length和Transfer-Encoding
来源: http://blog.csdn.net/superhosts/article/details/8737434 http://bbs.csdn.net/topics/390384017 对于h ...