P3905 道路重建
P3905 道路重建
我一开始想错了,我的是类似kruskal,把毁坏的边从小到大加,并且判断联通性。但是这有一个问题,你可能会多加,就是这条边没用,但是它比较小,你也加上了。
居然还有10分,数据也是水水的。。。
- #include<iostream>
- #include<cstdio>
- #include<queue>
- #include<algorithm>
- #include<cmath>
- #include<ctime>
- #include<cstring>
- #define inf 2147483647
- #define For(i,a,b) for(register int i=a;i<=b;i++)
- #define p(a) putchar(a)
- #define g() getchar()
- //by war
- //2017.10.10
- using namespace std;
- int n,m,dd,A,B,x,y,v,t;
- int a[][];
- bool b[][];
- int d[];
- int ans;
- struct node
- {
- int l,r,v;
- bool operator<(const node&aa)const
- {
- return v<aa.v;
- }
- }e[];
- void in(int &x)
- {
- char c=g();x=;
- while(c<''||c>'')c=g();
- while(c<=''&&c>='')x=x*+c-'',c=g();
- }
- void o(int x)
- {
- if(x>)o(x/);
- p(x%+'');
- }
- int find(int x)
- {
- if(d[x]==x)return x;
- d[x]=find(d[x]);
- return d[x];
- }
- int main()
- {
- in(n),in(m);
- For(i,,n)
- d[i]=i;
- For(i,,m)
- {
- in(x),in(y),in(v);
- a[x][y]=v;
- a[y][x]=v;
- }
- in(dd);
- For(i,,dd)
- {
- in(x),in(y);
- b[x][y]=true;
- b[y][x]=true;
- }
- in(A),in(B);
- For(i,,n)
- For(j,,n)
- {
- if(!b[i][j]&&a[i][j]>)
- d[find(i)]=find(j);
- if(b[i][j])
- {
- e[++t].l=i;
- e[t].r=j;
- e[t].v=a[i][j];
- }
- }
- sort(e+,e+t+);
- For(i,,t)
- {
- if(find(A)!=find(B))
- {
- if(find(e[i].l)!=find(e[i].r))
- {
- d[find(e[i].l)]=find(e[i].r);
- ans+=e[i].v;
- }
- }
- else
- {
- o(ans);
- break;
- }
- }
- return ;
- }
正解是把未坏的边的权值设成0,坏的边的值不变,跑spfa即可。
- #include<iostream>
- #include<cstdio>
- #include<queue>
- #include<algorithm>
- #include<cmath>
- #include<ctime>
- #include<cstring>
- #define inf 2147483647
- #define len 10010
- #define For(i,a,b) for(register int i=a;i<=b;i++)
- #define p(a) putchar(a)
- #define g() getchar()
- using namespace std;
- int n,m,x,y,v,dd,A,B;
- queue<int>q;
- int d[];
- bool vis[],b[][];
- struct node
- {
- int n,v;
- node *next;
- }*e[len];
- void push(int x,int y,int v)
- {
- node *p;
- p=new node();
- p->n=y;
- p->v=v;
- if(e[x]==NULL)
- e[x]=p;
- else
- {
- p->next=e[x]->next;
- e[x]->next=p;
- }
- }
- void in(int &x)
- {
- int y=;
- char c=g();x=;
- while(c<''||c>'')
- {
- if(c=='-')
- y=-;
- c=g();
- }
- while(c<=''&&c>='')x=x*+c-'',c=g();
- x*=y;
- }
- void o(int x)
- {
- if(x<)
- {
- p('-');
- x=-x;
- }
- if(x>)o(x/);
- p(x%+'');
- }
- void spfa(int x)
- {
- For(i,,n)
- d[i]=inf;
- d[x]=;
- q.push(x);
- node *p;
- int t;
- while(q.size()>)
- {
- t=q.front();
- p=e[t];
- vis[t]=true;
- while(p!=NULL)
- {
- if(!b[t][p->n])
- {
- if(d[t]<d[p->n])
- {
- d[p->n]=d[t];
- if(!vis[p->n])
- q.push(p->n);
- }
- }
- else
- {
- if(d[t]+p->v<d[p->n])
- {
- d[p->n]=d[t]+p->v;
- if(!vis[p->n])
- q.push(p->n);
- }
- }
- p=p->next;
- }
- vis[t]=false;
- q.pop();
- }
- }
- int main()
- {
- in(n),in(m);
- For(i,,m)
- {
- in(x),in(y),in(v);
- push(x,y,v);
- push(y,x,v);
- }
- in(dd);
- For(i,,dd)
- {
- in(x),in(y);
- b[x][y]=true;
- b[y][x]=true;
- }
- in(A),in(B);
- spfa(A);
- o(d[B]),p(' ');
- return ;
- }
P3905 道路重建的更多相关文章
- 洛谷——P3905 道路重建
P3905 道路重建 题目描述 从前,在一个王国中,在n个城市间有m条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有d条道路被破坏了.国王想要修复国家的道路系统,现 ...
- 洛谷 P3905 道路重建 题解
P3905 道路重建 题目描述 从前,在一个王国中,在\(n\)个城市间有\(m\)条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有\(d\)条道路被破坏了.国王想 ...
- P1359 租用游艇 && P3905 道路重建 ------Floyd算法
P1359 租用游艇 原题链接https://www.luogu.org/problemnew/show/P1359 P3905 道路重建 原题链接https://www.luogu.org/ ...
- 洛谷 P3905 道路重建
题目描述 从前,在一个王国中,在n个城市间有m条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有d条道路被破坏了.国王想要修复国家的道路系统,现在有两个重要城市A和B ...
- 洛谷P3905 道路重建
题目:https://www.luogu.org/problemnew/show/P3905 分析: 此题是显然的最短路算法,只是看到一起删掉的一堆边感到十分棘手,而且还要求出的是最短添加边的总长度 ...
- 【最短路】道路重建 @upcexam5797
时间限制: 1 Sec 内存限制: 128 MB 题目描述 小L的家乡最近遭遇了一场洪水,城市变得面目全非,道路也都被冲毁了.生活还要继续,于是市政府决定重建城市中的道路. 在洪水到来前,城市中共有n ...
- [JZOJ 5465] [NOIP2017提高A组冲刺11.9] 道路重建 解题报告 (e-dcc+树的直径)
题目链接: http://172.16.0.132/senior/#main/show/5465 题目: 小X所居住的X国共有n个城市,有m条无向道路将其连接.作为一个统一的国家,X 城的任意两个城市 ...
- 【洛谷P1272】道路重建
题目大意:给定一个 N 个节点的树,求至少剪掉多少条边才能使得从树中分离出一个大小为 M 的子树. 题解:考虑树形 dp,定义 \(dp[u][i][t]\) 为以 u 为根节点与前 i 个子节点构成 ...
- [JZOJ5465]道路重建--边双缩点+树的直径
题目链接 lueluelue 分析 这鬼题卡了我10发提交,之前做过一道类似的题目:https://rye-catcher.github.io/2018/07/09/luogu%E9%A2%98%E8 ...
随机推荐
- docker maven 出错:Failed to execute goal com.spotify:docker-maven-plugin:...: Request error: POST https://192.168.99.100:2376/build?t=
Spring Boot项目构建docker镜像,出错Failed to execute goal com.spotify:docker-maven-plugin:0.4.13:build (defau ...
- Windows完成端口与Linux epoll技术简介
收藏自:http://www.cnblogs.com/cr0-3/archive/2011/09/09/2172280.html WINDOWS完成端口编程1.基本概念2.WINDOWS完成端口的特点 ...
- saltstack主机管理项目【day23】:主机管理项目需求分析-设计
本节内容 一. 主机管理项目需求分析 二 .主机管理项目架构设计 三.主机管理项目初始构建 四. 主机管理项目编主机分发器 一. 主机管理项目需求分析 场景:我现在又一台裸机要实现一下人物 配置管理: ...
- Dubbo学习笔记4:服务消费端泛化调用与异步调用
本文借用dubbo.learn的Dubbo API方式来解释原理. 服务消费端泛化调用 前面我们讲解到,基于Spring和基于Dubbo API方式搭建简单的分布式系统时,服务消费端引入了一个SDK二 ...
- Java Web之路(二)Servlet之HttpServletResponse和HttpServletRequest
HttpServletResponse 1.告诉服务器应用使用UTF-8解析文本的两种方式,告诉客户端要使用什么编码 response.setHeader("content-type&quo ...
- nodejs图片总结
今天终于把朴灵老师写的<深入浅出Node.js>给学习完了, 这本书不是一本简单的Node入门书籍,它没有停留在Node介绍或者框架.库的使用层面上,而是从不同的视角来揭示Node自己内在 ...
- 【转】把Git Repository建到U盘上去
CHENYILONG Blog 把Git Repository建到U盘上去 转 把Git Repository建到U盘上去 Git很火.原因有三: 它是大神Linus Torvalds的作品,天然地具 ...
- 游程编码(Run Length Code)
一.什么是游程编码 游程编码是一种比较简单的压缩算法,其基本思想是将重复且连续出现多次的字符使用(连续出现次数,某个字符)来描述. 比如一个字符串: AAAAABBBBCCC 使用游程编码可以将其描述 ...
- Mysql备份文件
- python 获取二进制文件
import requests response = requests.get('https://www.baidu.com/aladdin/img/tools/ip.png')with open(' ...