当前位置:网站首页>1030 travel plan (30 points)
1030 travel plan (30 points)
2022-06-30 14:55:00 【Xue Dongjing】
maxmin
1030 Travel Plan (30 branch )
The question
Give an undirected graph , Find the shortest path . Multiple shortest paths output the least expensive .
Ideas
Find all the shortest paths ,dfs Find the least expensive .
It's similar to this **1018 Public Bike Management (30 branch )**
Code
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
typedef struct node{
int next,dis,v;
}node;
vector<node>mp[507];
vector<int> pre[507],path1,path2;
int vis[507],dis[507],minCost=0x3f3f3f,todis=0,mcost[507][507];
int n,m,s,d;
void djs(int x)
{
sizeof(vis,0,sizeof(vis));
memset(dis,0x3f3f3f,sizeof(dis));
dis[x]=0;
while(1){
int xmin=0x3f3f3f,temp=-1;
for(int i=0;i<n;i++){
if(vis[i]==0&&dis[i]<xmin){
xmin=dis[i];
temp=i;
}
}
if(temp==-1){
return ;
}
vis[temp]=1;
for(int i=0;i<mp[temp].size();i++){
int a=mp[temp][i].next;
if(vis[a]==0&&mp[temp][i].dis!=0x3f3f3f){
if(dis[a]>dis[temp]+mp[temp][i].dis){
dis[a]=dis[temp]+mp[temp][i].dis;
pre[a].clear();
pre[a].push_back(temp);
}else if(dis[a]==dis[temp]+mp[temp][i].dis){
pre[a].push_back(temp);
}
}
}
}
}
void dfs(int x)
{
if(x==s){
path1.push_back(x);
int cost=0;
for(int i=path1.size()-1;i>0;i--){
int p=path1[i];
int q=path1[i-1];
cost+=mcost[p][q];
}
if(cost<minCost){
minCost=cost;
path2=path1;
}
path1.pop_back();
return; // Notice the retuan, Don't forget .
}
path1.push_back(x);
for(int i=0;i<pre[x].size();i++){
dfs(pre[x][i]);
}
path1.pop_back();
}
int main()
{
node x;
int a,b,dis1,v;
scanf("%d%d%d%d",&n,&m,&s,&d);
for(int i=0;i<m;i++){
scanf("%d%d%d%d",&a,&b,&dis1,&v);
x.next=b;
x.dis=dis1;
mcost[a][b]=v;
mcost[b][a]=v;
x.v=v;
mp[a].push_back(x);
x.next=a;
x.dis=dis1;
x.v=v;
mp[b].push_back(x);
}
djs(s);
dfs(d);
for(int i=path2.size()-1;i>=0;i--){
printf("%d ",path2[i]);
}
printf("%d ",dis[d]);
printf("%d\n",minCost);
return 0;
}
边栏推荐
- Error $(...) size is not a function
- Binary rotation array (1)
- In situ merging of two arrays with two pointers
- Examples of bubble sorting and matrix element screening in MATLAB
- CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
- How to get palindrome number in MATLAB (using fliplr function)
- 2021 geek challenge Web
- Lihongyi machine learning 2020 homework summary
- Invalid argument during startup: Failed to open the . conf file: redis-window
- Shift operator (detailed)
猜你喜欢

DiceCTF - knock-knock

August 24, 2021 deque queue and stack

CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2

Knowledge learned from the water resources institute project

Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)

KnightCTF WEB
![[buuctf] [actf2020 freshman competition]exec1](/img/af/22051a5feb3c1f6d7201a483bde127.jpg)
[buuctf] [actf2020 freshman competition]exec1

【BUUCTF】 EasySql

CCF access control system (Full Score code + problem solving idea) 201412-1

Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
随机推荐
Double pointer circular linked list
In situ merging of two arrays with two pointers
DefCamp Capture the Flag (D-CTF) 2021-22 web
Analysis on the problems of irregular step hole on horizontal machining center
高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
Matlab calculates the factorial sum of the first n numbers (easy to understand)
Non decreasing column
day02
2021-07-15Caused by: org. quartz. ObjectAlreadyExistsException: Unable to store Job : ‘DEFAULT. TASK_ 1‘
V3 01_ Welcome
【BUUCTF】 EasySql
Vue returns to the previous page without refreshing the page / Vue caches the page
Matlab finds prime numbers within 100
Matlab finds a prime number that is greater than a given integer and follows this integer
JS array
Basic learning notes of C language
立式加工中心调试的步骤
JS to realize simple lottery function
Binary rotation array (1)
1134: Legal C identifier query