PAT1030. Travel Plan
//晴神模板,dij+dfs,貌似最近几年PAT的的图论大体都这么干的,现在还在套用摸板阶段。。。。估计把这及格图论题题搞完,dij,dfs,并查集就掌握差不多了(模板还差不多)为何bfs能自己干出来,dfs就各种跪。。。。感觉需要把图论的经典算法都码一遍,才能有更深的理解,现在只是浅表
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=501;
const int INF=1<<30;
int n,m,st,ed,g[maxn][maxn],cost[maxn][maxn];
bool vis[maxn]={false};
int dist[maxn],mincost=INF;
vector<int >pre[maxn];
vector<int>path,tmppath;
void dij(int s)
{
int i,j;
fill(dist,dist+maxn,INF);
for(i=0;i<n;i++)pre[i].push_back(i);
dist[s]=0;
for(i=0;i<n;i++)
{
int min=INF,u=-1;
for(j=0;j<n;j++)
{
if(!vis[j]&&dist[j]<min)
{
min=dist[j];
u=j;
}
}
if(u==-1)return ;
vis[u]=true;
for(int v=0;v<n;v++)
{
if(!vis[v]&&g[u][v]!=INF)
{
if(dist[v]>dist[u]+g[u][v])
{
dist[v]=dist[u]+g[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if(dist[v]==dist[u]+g[u][v])pre[v].push_back(u);
}
}
}
}
void dfs(int v)
{
int i;
if(v==st)
{
tmppath.push_back(v);
int tmpcost=0;
for(i=tmppath.size()-1;i>0;i--)
{
int cur=tmppath[i],next=tmppath[i-1];
tmpcost+=cost[cur][next];
}
if(tmpcost<mincost)
{
mincost=tmpcost;
path=tmppath;
}
tmppath.pop_back();
return ;
}
tmppath.push_back(v);
for(i=0;i<pre[v].size();i++)dfs(pre[v][i]);
tmppath.pop_back();
}
int main()
{
freopen("input.txt","r",stdin);
while(scanf("%d%d%d%d",&n,&m,&st,&ed)!=EOF)
{
int i,a,b;
fill(g[0],g[0]+maxn*maxn,INF);
fill(cost[0],cost[0]+maxn*maxn,INF);
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
scanf("%d%d",&g[a][b],&cost[a][b]);
g[b][a]=g[a][b];
cost[b][a]=cost[a][b];
}
dij(st);
dfs(ed);
for(i=path.size()-1;i>=0;i--)
printf("%d ",path[i]);
printf("%d %d\n",dist[ed],mincost);
}
return 0;
}
PAT1030. Travel Plan的更多相关文章
- PAT1030 Travel Plan (30)---DFS
(一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- PAT1030. Travel Plan (30)
#include <iostream> #include <limits> #include <vector> using namespace std; int n ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT_A1030#Travel Plan
Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- HDU 4014 Jimmy’s travel plan(图计数)
Jimmy’s travel plan Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
随机推荐
- ios之"performSelector may cause a leak because its selector is unknown"警告原因及其解决办法
问题描述 项目中使用到了从字符串创建选择器,编译时发现警告:"performSelector may cause a leak because its selector is unknown ...
- hdu1358 Period
首先给个博客:http://blog.csdn.net/lttree/article/details/20732385 感觉他说的很好,尤其是引用的那个博客,清晰的说明了循环节的两个公式. http: ...
- SCREAM:Error suppression ignored for
wamp报错SCREAM:Error suppression ignored for 问题:SCREAM:Error suppression ignored for 解决: 在php.ini最下面加入 ...
- 使用WebStorm/Phpstorm实现remote host远程开发
如果你的开发环境是在远程主机上,webstorm可以提供通过ftp/ftps/sftp等方式实现远程同步开发.这样我们可以就抛弃ftp. winscp等工具,通过webstorm编辑远程文件以及部署, ...
- tcpdump命令--实用篇
//查看本机与mysql的操作命令 注意 -i any表示监听所有网络接口,我们也根据自身情况选择网络接口 #tcpdump -i any -w - dst port 3306 |strings // ...
- Yii2.0 实现三级联动 [ 2.0 版本 ]
view中代码 <?php use yii\bootstrap\ActiveForm; /* @var $this yii\web\View */ /* @var $form yii\boots ...
- SPREAD_NET6
SPREAD_NET6 下载地址 http://www.gcpowertools.com.cn/downloads/trial/Spread.NET/EN_SPREAD_NET6_SETUP_RA_6 ...
- NPOI高效匯出Excel
using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using NPOI. ...
- 编写webpy程序,iep 报错,ulipad 运行正确
在web.py编程中,使用模板文件时,iep下会报错.ulipad不会报错. 用python 运行不报错. 在寻找答案.初步估计是iep的python运行环境有问题. 如图:
- [ActionScript3.0] 运用JPEGEncoderOptions或者PNGEncoderOptions保存图片到本地
在flash player 11.3和air3.3之前,我们可以借助第三方类(JPEGEncoder)这些,很容易处理.现在,有了encode和JPEGEncoderOptions这些,处理位图数据就 ...