当前位置:网站首页>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;
}
边栏推荐
- [buuctf] [actf2020 freshman competition]exec1
- PS cutting height 1px, Y-axis tiling background image problem
- Sum of CCF digits (full mark code + problem solving idea) 201512-1
- Finding the root of an integer by dichotomy
- K high frequency elements before sorting
- Bucket sorting (C language)
- 2021 geek challenge Web
- PS dynamic drawing
- 1131: genetic correlation
- 中信期货开户麻烦吗安全吗,期货开户手续费是多少,能优惠吗
猜你喜欢
ThinkPHP show method parameter controllable command execution
【BUUCTF】 Have Fun
Determine the number of digits of an integer in MATLAB (one line of code)
Detailed explanation of settimeout() and setinterval()
Matlab construction operation example
CCF access control system (Full Score code + problem solving idea) 201412-1
Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications
The first dark spring cup dnuictf
CCF command line options (Full Score code + problem solving ideas + skill summary) March 3, 2014
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
随机推荐
[extensive reading of papers] multi modal sarcasm detection and human classification in code mixed conversations
1137: encrypted medical record
Minimum covering substring of two pointers
K high frequency elements before sorting
[untitled]
Effect of shadow around the block after mouse over
val_ Loss decreases first and then increases or does not decrease but only increases
【BUUCTF】[GXYCTF2019]Ping Ping Ping1
1136: password translation
Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications
V3 03_ Getting started
2021-05-12
Knowledge learned from the water resources institute project
1134: Legal C identifier query
Invalid argument during startup: Failed to open the . conf file: redis-window
2021 geek challenge Web
Svn password forgetting solution
Thinkphp5 log file contains trick
Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
Matlab draws the image of the larger function value of the two functions (super simple)