Lightoj1002 【搜索】
题意:
两两之间的点的花费就是:从A点到B的一条路上某段的最大权值;给一个起点,求到各起点的最小花费。
思路:
一开始的思路:
n不是才500,我先建个图,然后DFS一下,不对,是2500;
如果直接暴搜,肯定T了。因为可能有一个环,然后你不能处理一个节点的向上节点。= =、T在这里,所以每次暴搜就相当于每次暴搜了整幅图;一开始写了一发,还以为再一次深刻理解DFS,然后T的我一脸懵逼,卧槽;不过还是加深了DFS的理解= =、。
①:如果要从DFS角度考虑,可以先求最小生成树,然后在树上DFS,主要是不存在环,比较方便;
②:另外一种就是最短路变形,spfa上直接搞搞就好了(这个还是要看对最短路的松弛熟练了没有);
思想还是 利用队列来操作,避免了重复的判断;
转化最小生成树的代码:
- #include<stdio.h>
- #include<queue>
- #include<string.h>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- typedef long long LL;
- const int INF=0x3f3f3f3f;
- const LL mod=1e9+7;
- const int N=5e2+10;
- struct edge{
- int x,y,w;
- };
- edge q[20000];
- int num;
- int pre[N];
- bool cmp(edge x,edge y)
- {
- return x.w<y.w;
- }
- struct asd{
- int to;
- int w;
- int next;
- };
- asd ma[20000];
- int head[20000],tol;
- int dis[N];
- bool vis[N];
- int n,m,t;
- void add(int a,int b,int c)
- {
- ma[tol].to=b;
- ma[tol].w=c;
- ma[tol].next=head[a];
- head[a]=tol++;
- }
- int Find(int x)
- {
- int r=x;
- while(pre[r]!=r)
- r=pre[r];
- int i=x,j;
- while(pre[i]!=r)
- {
- j=pre[i];
- pre[i]=r;
- i=j;
- }
- return r;
- }
- void init()
- {
- sort(q,q+num,cmp);
- for(int i=0;i<n;i++)
- pre[i]=i;
- tol=0;
- memset(head,-1,sizeof(head));
- for(int i=0;i<num;i++)
- {
- int fx=Find(q[i].x);
- int fy=Find(q[i].y);
- if(fx!=fy)
- {
- pre[fx]=fy;
- add(q[i].x,q[i].y,q[i].w);
- add(q[i].y,q[i].x,q[i].w);
- }
- }
- }
- void dfs(int u,int w)
- {
- for(int v=head[u];v!=-1;v=ma[v].next)
- {
- int i=ma[v].to;
- if(vis[i])
- continue;
- dis[i]=max(w,ma[v].w);
- vis[i]=true;
- dfs(i,dis[i]);
- }
- }
- int main()
- {
- int cas=1,T;
- int a,b,c;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&m);
- num=0;
- while(m--)
- {
- scanf("%d%d%d",&a,&b,&c);
- q[num].x=a;
- q[num].y=b;
- q[num++].w=c;
- }
- scanf("%d",&t);
- init();
- memset(vis,false,sizeof(vis));
- memset(dis,-1,sizeof(dis));
- vis[t]=true;
- dfs(t,0);
- printf("Case %d:\n",cas++);
- for(int i=0;i<n;i++)
- {
- if(i==t)
- puts("0");
- else if(dis[i]==-1)
- puts("Impossible");
- else
- printf("%d\n",dis[i]);
- }
- }
- return 0;
- }
最短路转化的代码:
- #include<stdio.h>
- #include<queue>
- #include<string.h>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- typedef long long LL;
- const int INF=0x3f3f3f3f;
- const LL mod=1e9+7;
- const int N=5e2+10;
- //struct asd{
- // int to;
- // int w;
- // int next;
- //};
- //asd q[N*N];
- //int tol,head[N*N];
- int ma[N][N];
- int dis[N];
- bool vis[N];
- int n,m,t;
- void spfa()
- {
- queue<int>q;
- for(int i=0;i<n;i++)
- {
- vis[i]=false;
- dis[i]=INF;
- }
- vis[t]=1;
- dis[t]=0;
- q.push(t);
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- vis[u]=0;
- for(int i=0;i<n;i++)
- {
- if(ma[u][i]==-1) continue;
- if(dis[i]>max(dis[u],ma[u][i]))
- {
- dis[i]=max(dis[u],ma[u][i]);
- if(!vis[i])
- {
- vis[i]=1;
- q.push(i);
- }
- }
- }
- }
- }
- int main()
- {
- int cas=1,T;
- int a,b,c;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&m);
- memset(ma,-1,sizeof(ma));
- while(m--)
- {
- scanf("%d%d%d",&a,&b,&c);
- if(ma[a][b]==-1)
- ma[a][b]=ma[b][a]=c;
- else
- ma[a][b]=ma[b][a]=min(c,ma[a][b]);
- }
- scanf("%d",&t);
- spfa();
- printf("Case %d:\n",cas++);
- for(int i=0;i<n;i++)
- {
- if(dis[i]==INF)
- puts("Impossible");
- else
- printf("%d\n",dis[i]);
- }
- }
- return 0;
- }
先
Lightoj1002 【搜索】的更多相关文章
- SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...
- solr_架构案例【京东站内搜索】(附程序源代码)
注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置
在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...
- bzoj1079--记忆化搜索
题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...
- bzoj3208--记忆化搜索
题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...
- Android中通过ActionBar为标题栏添加搜索以及分享视窗
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...
- 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做 ...
- Go语言实战 - 我需要站内搜索
山坡网的用户抱怨"为什么搜索'二鬼子李富贵'找不到'二鬼子汉奸李富贵'?我用百度搜都能找到." 当时我就滴汗了,用户说的有道理,应该要能搜索到. 之前的方案很简单,用户输入的字串会 ...
随机推荐
- C++ 中的几种初始化
前言 阅读C++教材时,想必你听过复制初始化,直接初始化,值初始化这三个概念吧.笔者本人常将其混淆,遂在此记录下它们的具体含义以便日后查阅. 复制初始化( copy-initialization ) ...
- android-async-http框架
android-async-http 简单介绍:An asynchronous, callback-based Http client for Android built on top of Apac ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- iOS进程间通信之CFMessagePort
本文转载至 http://www.cocoachina.com/industry/20140606/8701.html iOS系统是出了名的封闭,每个应用的活动范围被严格地限制在各自的沙盒中.尽管如此 ...
- linux环境下启动tomcat7出现时间过长(已经编译完成的项目)问题解决!
已经编译完成的项目,系统启动过程中,提示: INFO: Starting Servlet Engine: Apache Tomcat/7.0.81 Sep 20, 2017 3:17:32 PM or ...
- codeforces776D
传送门 这题的意思就是原本有一个长度为n的01串,再给出m的长度为n的01串,要求你判定是否可以通过原串与m个串中的某些串xor使得原串到达一个状态.n,m小于1e5. 这题最初我发现不可做,因为这貌 ...
- poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)
Rails Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25964 Accepted: 10199 Descripti ...
- 动态负载均衡(Nginx+Consul+UpSync)
Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...
- BZOJ 1617 [Usaco2008 Mar]River Crossing渡河问题:dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1617 题意: Farmer John以及他的N(1 <= N <= 2,500 ...
- UNP总结 Chapter 11 名字与地址转换
本章讲述在名字和数值地址间进行转换的函数:gethostbyname和gethostbyaddr在主机名字与IP地址间进行转换,getservbyname和getservbyport在服务器名字和端口 ...