当前位置: 首页 > wzjs >正文

建站服务是什么学网站建设多少学费

建站服务是什么,学网站建设多少学费,成都网站设计招聘,百度灰色关键词排名最短路 题目 903. 昂贵的聘礼 思路 如何建图&#xff1a; 对于每一个物品&#xff0c;建立一条由虚拟源点指向该物品的边对于可以替换的物品&#xff0c;建立一条替换物品到目标物品的边 最终就是求虚拟源点到终点 1 1 1 的边 代码 #include <iostream> #include …

最短路

题目

903. 昂贵的聘礼

思路

如何建图:

  1. 对于每一个物品,建立一条由虚拟源点指向该物品的边
  2. 对于可以替换的物品,建立一条替换物品到目标物品的边

最终就是求虚拟源点到终点 1 1 1 的边

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std;const int N = 110, INF = 0x3f3f3f3f;int n, m;
int g[N][N], dist[N];
bool st[N];
int level[N];
int ans = INF;void dijkstra(int l, int r)
{memset(st, false, sizeof st);memset(dist, 0x3f, sizeof dist);dist[0] = 0;// 因为多了一个虚拟源点0,所以要循环n+1次for(int i = 0; i <= n; i ++ ) {int t = -1;for(int j = 0; j <= n; j ++ ) // 还有虚拟源点 0if(!st[j] && (dist[j] < dist[t] || t == -1)) t = j;st[t] = true;if(t && (level[t] < l || level[t] > r)) // 特判虚拟源点continue;for(int j = 1; j <= n; j ++ ){if(level[j] < l || level[j] > r)    continue;dist[j] = min(dist[j], dist[t] + g[t][j]);}}ans = min(ans, dist[1]);
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);memset(g, 0x3f, sizeof g);for(int i = 0; i < N; i ++ ) g[i][i] = 0;cin >> m >> n;for(int i = 1; i <= n; i ++ ){int price, k;cin >> price >> level[i] >> k;g[0][i] = price;while(k -- ){int b, c;cin >> b >> c;g[b][i] = c;}}for(int i = level[1] - m; i <= level[1]; i ++ ) dijkstra(i, i + m);cout << ans << endl;return 0;
}

BFS

题目

矩阵距离
给定一个由 0 0 0, 1 1 1 构成的矩阵,让我们求每一个 0 0 0 1 1 1 的最短哈密顿距离。

思路
由题目描述,所有 1 1 1 的位置到 1 1 1 的最短哈密顿距离为 0 0 0,然后把所有 1 1 1 加入队列,这样每一个 0 0 0 第一次入队的距离就是最短的。

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std;typedef pair<int, int> PII;const int N = 1010;int n, m;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int a[N][N], b[N][N];
bool st[N][N];void bfs()
{memset(b, 0x3f, sizeof b);memset(st, false, sizeof st);queue<PII> q;//<==> 建立虚拟源点for(int i = 0; i < n; i ++ )for(int j = 0; j < m; j ++ )if(a[i][j] == 1){q.push({i, j});st[i][j] = true;b[i][j] = 0;}while(q.size()){auto t = q.front();q.pop();for(int i = 0; i < 4; i ++ ){int x = t.first + dx[i], y = t.second + dy[i];if(x < 0 || x >= n || y < 0 || y >= m || st[x][y] || a[x][y])  continue;b[x][y] = b[t.first][t.second] + 1;q.push({x, y});st[x][y] = true;}}
}int main()
{cin >> n >> m;for(int i = 0; i < n; i ++ ){string s;   cin >> s; //由于题目给定的数据之间没有空格,最好是直接读入一个字符串for(int j = 0; j < m; j ++ )    a[i][j] = s[j] - '0';}bfs();for(int i = 0; i < n; i ++ ){for(int j = 0; j < m; j ++ )   cout << b[i][j] << ' ';cout << endl;}return 0;
}

最短路

题目

选择最佳路线
题目让我们求多起点,单一终点的最短路

思路
我们是无法在一次最短路中求出多个起点到单一终点的最短距离的。
除非使用 F l o y d Floyd Floyd 算法,但时间复杂度不支持。

一、虚拟源点
把所有起点由一个虚拟源点连接起来,那么题目就转化成了求一个虚拟源点到单一终点的最短距离。
下面的代码是虚拟源点做法。

二、反向建图
题目让我们求多个起点到单一终点的最短的一条路径,我们可以反向建图,求单一终点所有起点中的距离的最小值。

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;typedef pair<int, int> PII;// M = 20010 会出错
const int N = 1010, M = 21010, INF = 0x3f3f3f3f;int n, m, S, cnt;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];void add(int a, int b, int c)  // 添加一条边a->b,边权为c
{e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}int dijkstra()
{memset(dist, 0x3f, sizeof dist);memset(st, 0, sizeof st);priority_queue<PII, vector<PII>, greater<PII> > q;q.push({0, 0});dist[0] = 0;while(q.size()){auto t = q.top();q.pop();int ver = t.second, distance = t.first;if(st[ver]) continue;st[ver] = true;for(int i = h[ver]; i != -1; i = ne[i]){int j = e[i];if(dist[j] > distance + w[i]){dist[j] = distance + w[i];q.push({dist[j], j});}}}return dist[S];
}int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);while(cin >> n >> m >> S){memset(h, -1, sizeof h);idx = 0;while (m -- ){int a,  b, c;cin >> a >> b >> c;add(a, b, c);}cin >> cnt;while(cnt -- ){int x;  cin >> x;add(0, x, 0);}int t = dijkstra();if(t == INF)    t = -1;cout << t << endl;}return 0;
}

最小生成树

题目

新的开始
题目说明该图可能存在多个联通块,求这些连通块的最小生成树之和

思路
因为我们在每个连通块中都需要设置一个起点( d i s t [ S ] = 0 dist[S]=0 dist[S]=0),所以说这题就是一个多起点问题,多起点就要立马想到虚拟源点。
由于我们设置了一个虚拟源点,那么点数就会多 1 1 1,所以在求最小生成树的时候,循环次数也要加 1 1 1

代码

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 310;int n;
int g[N][N];
int dist[N];
bool st[N];int prim()
{memset(dist, 0x3f, sizeof dist);dist[0] = 0;int res = 0;for(int i = 0; i <= n; i ++ ){int t = -1;for(int j = 0; j <= n; j ++ )if(!st[j] && (t == -1 || dist[j] < dist[t]))    t = j;st[t] = true;res += dist[t];for(int j = 0; j <= n; j ++ )   dist[j] = min(dist[j], g[t][j]);}return res;
}int main()
{memset(g, 0x3f, sizeof g);for(int i = 0; i < N; i ++ )    g[i][i] = 0;cin >> n;for(int i = 1; i <= n; i ++ )   {int x;  cin >> x;g[0][i] = g[i][0] = x;}for(int i = 1; i <= n; i ++ )for(int j = 1; j <= n; j ++ )cin >> g[i][j];cout << prim() << endl;return 0;
}



总结

起点和终点划分最短路

单一起点,多个(单一)终点:(普通单源最短路)

多个起点,单一终点

  1. 反向求最短路,转化为单一起点,多个终点
  2. 创建虚拟源点,转化为单一起点,单一终点最短路。

多个起点,多个终点

  1. 创建虚拟源点,转化成单一起点,多个终点最短路
  2. 创建虚拟汇点,转化为多个起点,单一终点最短路,然后反向求最短路

区分这里的多起点多终点的含义:在起点的集合和终点的集合中各选取一个起点和终点的最短路径,并不是让我们求每个起点到每个终点的最短路径。

细节问题

由于我们在创建虚拟源点或者虚拟汇点的时候,需要额外的边用来连接虚拟源点,所以边数需要多开 N N N 条边。


文章转载自:

http://LRwQXtC0.mjtft.cn
http://E5jT7Law.mjtft.cn
http://DYZ3nICo.mjtft.cn
http://eq9xVR0I.mjtft.cn
http://6667hF4B.mjtft.cn
http://PiubHHV4.mjtft.cn
http://v5oY30QR.mjtft.cn
http://ZfTXWrmC.mjtft.cn
http://1s5iesZK.mjtft.cn
http://nBei1JxJ.mjtft.cn
http://KSGAgPzr.mjtft.cn
http://YDHbwB58.mjtft.cn
http://4Ji698xK.mjtft.cn
http://PpK7yIYP.mjtft.cn
http://y2qNhlgO.mjtft.cn
http://bX9MaIPR.mjtft.cn
http://fbIid3dZ.mjtft.cn
http://eLYgLT5W.mjtft.cn
http://Z4E6TTOK.mjtft.cn
http://XEZfDbQt.mjtft.cn
http://ZDQWwydp.mjtft.cn
http://xQqGSTaR.mjtft.cn
http://OeCirYFQ.mjtft.cn
http://9pZulGVg.mjtft.cn
http://1L2jHRUJ.mjtft.cn
http://c3fRByMo.mjtft.cn
http://zpHgrYk1.mjtft.cn
http://VFDQZEA0.mjtft.cn
http://fWHq7cmv.mjtft.cn
http://DSJaZkQC.mjtft.cn
http://www.dtcms.com/wzjs/746745.html

相关文章:

  • 淘宝客推广平台湖南纯手工seo电话
  • 网站优化的意义沧州微酷网络科技有限公司
  • 目前网站开发语言企业建站系统下载
  • 介绍做燕窝的网站网站正在建设中 源码下载
  • 平面设计网站推荐网站建设新闻中心
  • 个人网站设计论文的结论wordpress搬家教程
  • 佛山网站免费制作vps上创建网站
  • 有什么超好用的做简历的网站兴宁网站建设
  • 昆明个人网站建设平台上海外贸官网
  • 建设网站需要多少钱济南兴田德润o厉害吗网络公司网站开发
  • 手机销售网站的设计与实现微信公众号自定义菜单wordpress
  • 做调查的网站推荐网站维护更新
  • 网络宣传网站建设价格wordpress插件文件夹在哪
  • 做进行网站推广赚钱上海迈诺网站建设
  • 网站建设设计制美克美家网站建设
  • 谷歌seo快速排名优化方法市场seo是什么
  • 建立自己的平台网站吗广州网站建设网络
  • 晋城中英文网站建设wordpress英文版改中文
  • 中小型网站建设哪家好网站绑定域名
  • 网站开始是怎么做的多店铺开源商城系统
  • 这个网站做海外推广百度网站优点
  • 广告设计效果图电脑系统优化工具
  • 南宁制作网站公司做外贸女装有哪些网站有哪些
  • 免费的简历制作网站西安网站建设需要多少钱
  • 网站开发费用怎么入账wordpress演示数据包
  • 服装平台网站有哪些曲靖模板网站建设
  • 官方网站的网络营销功能分析网站建设保教
  • 贵州建设监理协会网站用C语言做网站登录界面
  • 阿里云nas做网站医院网站asp
  • 如何给网站做seo开平市网站建设