当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

实用系列丨免费可商用视频素材库
![洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表](/img/4a/ab732c41ea8a939fa0983fec475622.png)
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表

请问大家在什么网站上能查到英文文献?

Mapper agent development

35页危化品安全管理平台解决方案2022版

How to set automatic reply for mailbox and enterprise mailbox?

PR FAQ, what about PR preview video card?

130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth

What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail

Explain in detail the process of realizing Chinese text classification by CNN
随机推荐
Mapper agent development
Digital twin smart factory develops digital twin factory solutions
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
Which websites can I search for references when writing a thesis?
经济学外文文献在哪查?
[array] binary search
leetcode 650. 2 keys keyboard with only two keys (medium)
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
教育学大佬是怎么找外文参考文献的?
How much do you know about synchronized?
MFC文件操作
How to write the design scheme of the thesis?
容器运行时分析
Unique line of "Gelu"
秒杀系统设计
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
接口自动化覆盖率统计——Jacoco使用
JVM foundation review
Is the multitasking loss in pytoch added up or backward separately?
TypeError: Cannot read properties of undefined (reading ***)