当前位置:网站首页>Pat 1030 travel plan (30 points) (unfinished)
Pat 1030 travel plan (30 points) (unfinished)
2022-07-03 00:16:00 【Python ml】
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n,m,s,d;
int e[510][510],dis[510],cost[510][510];
vector<int>pre[510];
bool visit[510];
const int inf = 99999999;
vector<int> path, temppath;
int mincost = inf;
void dfs(int v){
temppath.push_back(v);
if(v==s){
// Traverse to the end , Calculation selection cost The smallest path
int tempcost=0;
for(int i=temppath.size()-1;i>=0;i--){
int id=temppath[i],nextid=temppath[i-1];
tempcost+=cost[id][nextid];
}
if(tempcost<mincost){
// Update cost minimum path
mincost=tempcost;
path=temppath;
}
temppath.pop_back();
return;
}
for(int i=0;i<pre[v].size();i++)
dfs(pre[v][i]);
temppath.pop_back(); // adopt v The path of point cannot be traversed s Recursively pop up the currently accessed node layer by layer v
}
int main() {
fill(e[0], e[0] + 510 * 510, inf);
fill(dis, dis + 510, inf);
scanf("%d%d%d%d", &n, &m, &s, &d);
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
scanf("%d",&e[a][b]); //e[a][b] by a b Between dis
e[b][a]=e[a][b];
scanf("%d",&cost[a][b]);
cost[a][b]=cost[b][a];
}
pre[s].push_back(s);
dis[s]=0;
//Dijksta, Each cycle 1. Find the shortest path from the current starting point to each point dis[u] 2. The update may be through u The shortest path to each point
for(int i=0;i<n;i++){
//Dijksta Record path pre Array , Determine the shortest path of one point at a time , Including self circulation n Time
int u=-1,minn=inf;
for(int j = 0; j < n; j++) {
if(visit[j] == false && dis[j] < minn) {
// Traverse the current node to each point of the undetermined path dis[j] The shortest distance in , Among them to u The node distance is the shortest
u = j;
minn = dis[j];
}
}
if(u == -1) break;
visit[u] = true; // The shortest path to this point has been determined
for(int v=0;v<n;v++){
// Update the starting point through u The current shortest distance from point to point , The first cycle updates the distance from the starting point to each point
if(visit[v]==false&&e[u][v]!=inf){
if(dis[v]>dis[u]+e[u][v]){
// after u O 'clock v Is the distance between points closer
dis[v]=dis[u]+e[u][v];
pre[v].clear(); // If the path is updated , Clear the original path source ,v The previous node array in the shortest path of is pressed u node
pre[v].push_back(u);
}else if(dis[v]==dis[u]+e[u][v]){
// The shortest path is not unique
pre[v].push_back(u);
}
}
}
}
dfs(d);
for(int i=path.size()-1;i>=0;i--)
cout<<path[i]<<" ";
cout<<dis[d]<<" "<<mincost;
system("pause");
return 0;
}
边栏推荐
- [array] binary search
- 35 pages dangerous chemicals safety management platform solution 2022 Edition
- Define MySQL function to realize multi module call
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- 返回二叉树中最大的二叉搜索子树的根节点
- Sourcetree details
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
- Interface difference test - diffy tool
- Sysdig analysis container system call
- 教育学大佬是怎么找外文参考文献的?
猜你喜欢

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
![[shutter] open the third-party shutter project](/img/1a/e35d0180612d7e79b55e7818193740.jpg)
[shutter] open the third-party shutter project

How to set automatic reply for mailbox and enterprise mailbox?

Custom throttling function six steps to deal with complex requirements

TypeError: Cannot read properties of undefined (reading ***)

接口差异测试——Diffy工具
![洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表](/img/4a/ab732c41ea8a939fa0983fec475622.png)
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表

67 page overall planning and construction plan for a new smart city (download attached)

Difference between NVIDIA n card and amda card

Flexible combination of applications is a false proposition that has existed for 40 years
随机推荐
直击产业落地!飞桨重磅推出业界首个模型选型工具
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
How much do you know about synchronized?
Develop knowledge points
Request and response
Is there a specific format for English papers?
Difference between NVIDIA n card and amda card
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Maybe you read a fake Tianlong eight
MATLAB signal processing [Q & a notes-1]
MySQL advanced learning notes (4)
zhvoice
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
监控容器运行时工具Falco
Additional: token; (don't read until you finish writing...)
MySQL advanced learning notes (III)
sysdig分析容器系统调用
Returns the root node of the largest binary search subtree in a binary tree
List of major chip Enterprises
Which software can translate an English paper in its entirety?