Code vs 1391 伊吹萃香(虫洞)
来源:互联网 发布:linux 递归删除目录 编辑:程序博客网 时间:2024/05/29 06:37
QAQ
思路:SPFA+分层图
dis[i][j]表示第i个节点在第j个时间的最短路径
可以看出j并不需要全部的记下来,我们只需要用0,1来记录时间就行了
同理vis数组同样两维
根据题目要求拓展的花费,注意处理等待的情况
优化,双向队列,如果当前拓展出的节点dis比队首小,就将这个节点放在队首,这样就可以更快的拓展出最终的状态辣
#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>#include<cstring>using namespace std;const int maxn=60005; struct dqs{ int f,t,c;}hh[maxn];struct dqm{ int pos; bool bw;};int first[maxn],next[maxn];int tot=0;void build(int f,int t,int c){ hh[++tot]=(dqs){f,t,c}; next[tot]=first[f]; first[f]=tot;}bool color[maxn],used[maxn][2];int dis[maxn][2],w[maxn],s[maxn];int cha(int f,int t,int bow){ int fcha=color[f]^bow; int tcha=color[t]^bow; if(fcha==tcha) return 0; else { int cha=abs(w[f]-w[t]); if(fcha==1&&tcha==0) return cha; else return -cha; }}deque<dqm>q;void spfa(int st){ dqm begin; begin.pos=st; begin.bw=0; q.push_back(begin); dis[st][0]=0; used[st][0]=1; while(!q.empty()) { dqm head=q.front(); q.pop_front(); used[head.pos][head.bw]=0; for(int i=first[head.pos];i;i=next[i]) { int u=hh[i].t; dqm now; now.pos=u; now.bw=head.bw^1; if(dis[u][now.bw]>dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0)) { dis[u][now.bw]=dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0); if(!used[u][now.bw]) { if(q.size()!=0) { if(dis[u][now.bw]<=dis[q.front().pos][q.front().bw]) q.push_front(now); else q.push_back(now); } else q.push_back(now); used[u][now.bw]=1; } } } if(dis[head.pos][head.bw^1]>dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw)) { dis[head.pos][head.bw^1]=dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw); if(!used[head.pos][head.bw^1]) { used[head.pos][head.bw^1]=1; if(q.size()!=0) { if(dis[head.pos][head.bw^1]<=dis[q.front().pos][q.front().bw]) q.push_front((dqm){head.pos,head.bw^1}); else q.push_back((dqm){head.pos,head.bw^1}); } else q.push_back((dqm){head.pos,head.bw^1}); } } }}int main(){ int n,m,f,t,c; scanf("%d%d",&n,&m); memset(dis,0x3f,sizeof(dis)); for(int i=1;i<=n;i++) scanf("%d",&color[i]); for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int i=1;i<=n;i++) scanf("%d",&s[i]); for(int i=1;i<=m;i++) { scanf("%d%d%d",&f,&t,&c); build(f,t,c); } spfa(1); printf("%d",min(dis[n][0],dis[n][1])); return 0;}
阅读全文
0 0
- Code vs 1391 伊吹萃香(虫洞)
- CODE【VS】2519 (模拟)
- VS Code
- VS Code
- VS Code
- VS Code
- VS Code
- vs code
- VS Code
- vs code (visual studio code)配置git和markdown
- 【CTSC1999】 CODE[VS] 2218 补丁VS错误(状压最短路)
- 【CODE VS】P1695 windows 2013(DP)
- CODE[VS] 1036 商务旅行(LCA + BFS)
- code vs 1378 选课(树形DP)
- code vs 1743 反转卡片(splay)
- code vs 1106 篝火晚会(置换)
- code vs 1173 最优贸易 (spfa)
- code vs 1711 棋盘分割 (dp)
- UrhoSharp iOS和tvOS 开发入门
- 使用 RMI + ZooKeeper 实现远程调用框架
- vsftpd.conf.5配置文件手册(中文版)
- HierarchicalBeanFactory
- double运算精度问题
- Code vs 1391 伊吹萃香(虫洞)
- First blog
- PHP时间戳和日期转换
- 漫画赏析:Linux 内核到底长啥样
- hdu6092
- 编程实用小技巧
- Butter Knife 8.8.1的安装和使用
- CentOS下卸载OpenJDK,配置安装Sun JDK
- 并查集详解