【TYVJ 五月图论专项有奖比赛】
最短路+TSP+最小生成树+倍增LCA+TreeDP
第一题
其实是个TSP问题(然而我没发现),但是关键点很少,只有5个,所以用dij+heap分别预处理出来这五个点为源的最短路……
然后枚举起点 i ,枚举这5个点的经过顺序,然后O(1)处理答案就可以了……
容易写错的地方是 五个点的标号(a[i]),以及第几个点(i),这个地方容易搞混……
我爆零的地方是:有个地方原来写的是 i 循环,然而我修改了以后内层变成 j 循环了,但是我没改循环内的语句……
还有就是数组应该开 len[M<<1]的,我开成len[N<<1] 了。
#include<queue>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std; int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>'') {if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<='') {v=v*+ch-''; ch=getchar();}
return v*sign;
}
typedef long long LL;
const int N=,M=,INF=~0u>>;
/*******************template********************/
int to[M<<],next[M<<],len[M<<],head[N],cnt;
void add(int x,int y,int z){
to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt; len[cnt]=z;
to[++cnt]=x; next[cnt]=head[y]; head[y]=cnt; len[cnt]=z;
} int n,m,k;
int a[],b[],d[][N];
bool vis[N],sign[N];
struct node{int x,d;};
bool operator < (node a,node b){return a.d>b.d;}
priority_queue<node>Q; void dij(int s){
memset(d[s],0x3f,sizeof d[s]);
memset(vis,,sizeof vis);
d[s][a[s]]=;
Q.push((node){a[s],});
while(!Q.empty()){
int x=Q.top().x; Q.pop();
if (vis[x]) continue;
vis[x]=;
for(int i=head[x];i;i=next[i])
if (d[s][to[i]]>d[s][x]+len[i]){
d[s][to[i]]=d[s][x]+len[i];
Q.push((node){to[i],d[s][to[i]]});
}
}
}
int ans=1e9+;
void check(){
int tmp=;
F(i,,n){
tmp=;
if (!sign[i]){
tmp+=d[b[]][i]+d[b[k]][i];
F(j,,k-) tmp+=d[b[j]][ a[b[j+]] ];
ans=min(ans,tmp);
}
}
}
bool chk[];
void dfs(int x){
if (x==k+) check();
F(i,,k) if (!chk[i]){
b[x]=i;
chk[i]=;
dfs(x+);
b[x]=;
chk[i]=;
}
}
int main(){
n=getint(); m=getint(); k=getint();
F(i,,k){
a[i]=getint();
sign[a[i]]=;
}
F(i,,m){
int x=getint(),y=getint(),z=getint();
add(x,y,z);
}
F(i,,k) dij(i);
dfs();
printf("%d\n",ans);
return ;
}
第二题
跟NOIP 2012的d1t3(希望没记错时间……那题叫货车运输……)几乎完全相同的题,只不过那题是找最小,这题是最大。
搞一个最大生成树,然后在树上倍增就可以了。
40分?……因为忘记判impossible了……2333
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
return r*v;
}
const int N=1e5+,M=3e5+;
/*******************template********************/ int to[N<<],next[N<<],head[N],len[N<<],cnt;
void add(int x,int y,int z){
to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt; len[cnt]=z;
to[++cnt]=x; next[cnt]=head[y]; head[y]=cnt; len[cnt]=z;
}
struct edge{int u,v,w;}E[M];
bool cmp(const edge &a,const edge &b){return a.w<b.w;}
int f[N],sz[N];
inline int getf(int x){return f[x]==x ? x : getf(f[x]);} int fa[N][],mx[N][],dep[N],n,m;
void dfs(int x){
F(i,,)
if (dep[x]>=(<<i)){
fa[x][i]=fa[fa[x][i-]][i-];
mx[x][i]=max(mx[fa[x][i-]][i-],mx[x][i-]);
}else break;
for(int i=head[x];i;i=next[i])
if (to[i]!=fa[x][]){
fa[to[i]][]=x;
dep[to[i]]=dep[x]+;
mx[to[i]][]=len[i];
dfs(to[i]);
}
}
int query(int x,int y){
if (dep[x]<dep[y]) swap(x,y);
int t=dep[x]-dep[y];
int ans=;
F(i,,) if (t&(<<i)){
ans=max(mx[x][i],ans);
x=fa[x][i];
}
D(i,,) if (fa[x][i]!=fa[y][i]){
ans=max(mx[x][i],ans);
ans=max(mx[y][i],ans);
x=fa[x][i]; y=fa[y][i];
}
if (x!=y) ans=max(ans,max(mx[x][],mx[y][]));
return ans;
}
int main(){
n=getint(); m=getint();
F(i,,m){
int x=getint(),y=getint(),z=getint();
E[i]=(edge){x,y,z};
}
sort(E+,E+m+,cmp);
F(i,,n) f[i]=i,sz[i]=;
F(i,,m){
int f1=getf(E[i].u),f2=getf(E[i].v);
if (f1!=f2){
if (sz[f1]>sz[f2]) swap(f1,f2);
f[f1]=f2;
sz[f2]+=sz[f1];
add(E[i].u,E[i].v,E[i].w);
}
} dfs();
int T=getint();
while(T--){
int x=getint(),y=getint();
if (getf(x)!=getf(y)) puts("impossible");
else printf("%d\n",query(x,y));
}
return ;
}
第三题
我并不会写……只好预处理出来dist[i][j]数组,然后$N^2$枚举+O(n)判断……三次方的做法骗了30分= =
然而正解是找树的重心?预处理出来每棵子树的答案,由于整棵树分成的两部分是一棵完整的子树+剩下的部分,所以枚举那个砍掉的子树?在再加上一些神奇的技巧……
然而一上午也没yy出来aaarticlea/gif;base64,R0lGODlhQgBCAOYAAP////z9/ff7/Pn5+fT6+/f29vD4+vX19fb19ez19/L09fT08/Ly8uj09vDw8Obv8eDx8+7u7e3s7Ozr6tzw8urr6+Ds7unp6dnt8Onn5+fn593q7Nno69Lr78/q7uTk5eTi4dzk5cvo7NXl5+Hg4N/f397e3dHh48zi5cHj6Nvb273g5tPZ2tvX18jb3tvX1rTb49TT08TV19LR0NHNzLnT16jX3s7KyLvN0MvIx5/S28fIyK3N0bvKy8jEwpTO2MXBv5fK0qXGyr+9vI7J0766uJrCyLu3trm1tIHCzLezs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T8ERdSEgyBAu0zOiX8D+DBAEhAkJ4YTn5kALyGUvBqSAQ1wmsr8CBGB/DegNd6QgoxlZMAJ1M8gHakGTd8jtOHdxQiLWUJwSZa4FgwkBBbbTsCWUQCkXxCBZDhJATnQQAMA7yBBAMocpfCAwgoDGGGLAuguFIE9SuEsJQPCBJjaxDW0Yngo0ED1N0AAyx7GGIsJwgxmlbzNzgN0gZqA033SBBe7T4Ry2gBcNOGAAmRiAAypTAiiMEAgXhKMgImCCKGWjFFsIAttqMLfKDGYI8huC+5iIFxCYwI5zWEMRYpg+HAbmA0CAgiah8L/AnCBmCm4YAzAeqKwmakADANGQD9x3xSlgIwyTjGENBTGZGNryBlmQxGxUwYoWuMIKPWgCG0ABMpAlQhFrmEJxbOmAKsrNltAUDxCmEAZKTCIMMujACD5wA02G4ZtZgEIRTBDNCFQSAIEAADs=" alt="" />果然代码能力就是渣渣,sad……
UPD:2015年5月20日 16:28:33
Orz zyf神犇
果然是……枚举断了哪条边,然后暴力修改= =用dfs整棵树来选边就可以,从当前点沿fa修改上去,两半树各求一遍答案……ok……无限ym啊……
//TYVJ C
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
return r*v;
}
const int N=;
/*******************template********************/
int to[N<<],next[N<<],head[N],cnt;
void add(int x,int y){
to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt;
to[++cnt]=x; next[cnt]=head[y]; head[y]=cnt;
} int n,a[N],f[N][],dep[N],s[N],fa[N],cut;
LL g[N],ans=1e15;
void dfs(int x){
for(int i=head[x];i;i=next[i])
if (to[i]!=fa[x]){
dep[to[i]]=dep[x]+;
fa[to[i]]=x;
dfs(to[i]);
s[x]+=s[to[i]];
if (s[to[i]]>s[f[x][]]) f[x][]=f[x][],f[x][]=to[i];
else if (s[to[i]]>s[f[x][]]) f[x][]=to[i];
g[x]+=g[to[i]]+s[to[i]];
}
}
LL getans(int x,int cnt){
int t=(f[x][]==cut || s[f[x][]]>s[f[x][]]) ? f[x][] : f[x][];
if (*s[t]>cnt) return *s[t]-cnt+getans(t,cnt);
else return ;
}
void dp(int x){
for(int i=head[x];i;i=next[i])
if (to[i]!=fa[x]){
cut=to[i];
for(int j=x;j;j=fa[j]) s[j]-=s[to[i]];
ans=min(ans,g[]-g[to[i]]-(LL)s[to[i]]*dep[to[i]]-getans(,s[])+g[to[i]]-getans(to[i],s[to[i]]));
for(int j=x;j;j=fa[j]) s[j]+=s[to[i]];
dp(to[i]);
}
} int main(){
#ifndef ONLINE_JUDGE
freopen("C.in","r",stdin);
freopen("C.out","w",stdout);
#endif
n=getint();
F(i,,n){
int x=getint(),y=getint();
add(x,y);
}
F(i,,n) s[i]=getint();
dfs();
dp();
printf("%lld\n",ans);
return ;
}
【TYVJ 五月图论专项有奖比赛】的更多相关文章
- 【tyvj五月有奖赛 暨Loi 55 Round #1】
解题报告: 傻逼错误天天犯QAQ 第一题:简单DP,f[i][j]表示第 i 道题选 j 的最大得分,可以从f[i-1][j-1],f[i-1][j],f[i-1][j+1]转移过来,其实是可以滚动数 ...
- DP思路
在这里记录一些在大神们的博客,以及自己做过的一些DP的神奇思路吧 1.2015/04 NEUQ 月赛 转自:http://zyfzyf.is-programmer.com/posts/89993.h ...
- 【ContestHunter】【弱省胡策】【Round4】
01分数规划(网络流)+状压DP+树形DP 官方题解地址:http://pan.baidu.com/s/1mg5S5z6 A 好神啊= =第一次写01分数规划 其实分数规划是要求$$ Maximize ...
- 准备熟悉Kaggle -菜鸟进阶
原文链接http://www.bubuko.com/infodetail-525389.html 1.Kaggle简介 Kaggle是一个数据分析的竞赛平台,网址:https://www.kaggle ...
- 膜拜acm大牛 虽然我不会这题,但是AC还是没有问题的~(转自hzwer)
wywcgs: 亦称Lord Wu,俗名吴垠,2009级厦门大学智能科学与技术学院研究生,本科就读于哈尔滨工业大学.因其深厚的算法功底与独到的思维方式,被尊为“吴教主”,至今声威犹存. 2006年起参 ...
- 大数据竞赛平台——Kaggle 入门
Reference: http://blog.csdn.net/witnessai1/article/details/52612012 Kaggle是一个数据分析的竞赛平台,网址:https://ww ...
- 大数据竞赛平台——Kaggle 入门篇
这篇文章适合那些刚接触Kaggle.想尽快熟悉Kaggle并且独立完成一个竞赛项目的网友,对于已经在Kaggle上参赛过的网友来说,大可不必耗费时间阅读本文.本文分为两部分介绍Kaggle,第一部分简 ...
- [心得]暑假DAY1 | 7-7考试总结
呼.. 正式开始暑假集训. 今天一上午还在搞7-7的考试改题 然而,该来该去,TLE48过不去了 不知道哪的问题,loj上1w3ms(卡常都没能救得了) 至于T1和T3,简单总结一下算了 排序 感觉很 ...
- XJOI 夏令营501-511NOIP训练18 高三楼
参观完各种饭堂,学校还有什么著名的景点呢?当然是教室了,此时此刻我 们来到了高三楼.你会发现高三楼门口会有以身份认证系统,这东西还有着一段疼人的历史.每年的九月到来,高三的童鞋大多不习惯学校的作息时间 ...
随机推荐
- Linux 下开发环境的搭建(软件安装)
一.Centos6.5下安装JDK 步骤1: 查看现有安装的JDK版本 rpm –qa | grep -i java 步骤2: 卸载已有软件 rpm -e --nodeps java-1.7.0-op ...
- java.lang.NoClassDefFoundError: ognl/PropertyAccessor
本篇对 Web 开发中,项目部署后.开启 Tomcat 服务器 Console 控制台报错 java.lang.NoClassDefFoundError: ognl/PropertyAccessor ...
- 10 Best jQuery and HTML5 WYSIWYG Plugins
https://www.sitepoint.com/10-best-html-wysiwyg-plugins/
- 详谈 Spring 中的 IOC 和 AOP
这篇文章主要讲 Spring 中的几个点,Spring 中的 IOC,AOP,下一篇说说 Spring 中的事务操作,注解和 XML 配置. Spring 简介 Spring 是一个开源的轻量级的企业 ...
- poj3349(hash table)
做的第一道哈希表的题目.速度很慢,跑了3000+ms.采用六条边的和对一个大质数的余数作为哈希表的key,理论上质数取得越大,消耗的空间就越大,但是速度会加快,这里取了14997.地址冲突用链表解决. ...
- Django小知识点整理
一.Django中Choices字段显示中文的两种方式 1.get_xxxx_display():这种方式只能针对对象来使用 {% for row in server_list %} <li&g ...
- Centos7下shell脚本添加开机自启动
添加开机自启脚本,注意都需要用绝对路径 psubscribe.sh脚本中的内容: nohup /usr/bin/php -f /data/aliyun51015cn/redisChannel/psub ...
- luoguP4783 [模板]矩阵求逆 线性代数
求\(n^2\)的矩阵的逆 翻了翻题解,看到了初等矩阵这个东西,突然想起来在看线代的时候看到过.... 然后又温习了一遍线性代数的知识 不妨设\(PA = E\),其中\(P\)是一堆初等矩阵的积(必 ...
- [HDU6155]Subsequence Count
题目大意: 给定一个01序列,支持以下两种操作: 1.区间反转: 2.区间求不同的子序列数量. 思路: 首先我们考虑区间反转,这是一个经典的线段树操作. 接下来考虑求不同的子序列数量,在已知当前区间的 ...
- hdu 3338 最大流 ****
题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于 ...