PAT (Advanced Level) 1030. Travel Plan (30)
先处理出最短路上的边。变成一个DAG,然后在DAG上进行DFS。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=;
int n,m,C1,C2,tot,ans1,ans2;
int val[maxn];
struct Edge
{
int u,v,L,cost;
}e[];
int f[];
vector<int>g[maxn];
int dis[][maxn];
int h[maxn]; int path[maxn],ans[maxn];
int cnt; void init()
{
tot=;
for(int i=;i<=;i++) g[i].clear();
memset(h,,sizeof h);
} void add(int a,int b,int c,int d)
{
e[tot].u=a,e[tot].v=b,e[tot].L=c,e[tot].cost=d;
g[a].push_back(tot);
tot++;
} void read()
{
scanf("%d%d%d%d",&n,&m,&C1,&C2);
for(int i=;i<=m;i++)
{
int u,v,L,cost; scanf("%d%d%d%d",&u,&v,&L,&cost);
add(u,v,L,cost);
add(v,u,L,cost);
}
} void SPFA(int f,int st)
{
for(int i=;i<=;i++) dis[f][i]=INF;
dis[f][st]=;
int flag[maxn]; memset(flag,,sizeof flag);
queue<int>Q; Q.push(st); flag[st]=;
while(!Q.empty())
{
int head= Q.front(); Q.pop(); flag[head]=;
for(int i=;i<g[head].size();i++)
{
int id=g[head][i];
if(dis[f][head]+e[id].L<dis[f][e[id].v])
{
dis[f][e[id].v]=dis[f][head]+e[id].L;
if(flag[e[id].v]==)
{
Q.push(e[id].v);
flag[e[id].v]=;
}
}
}
}
} void dfs(int x,int deep,int sum)
{
path[deep]=x;
if(x==C2)
{
if(sum<ans2)
{
ans2=sum;
cnt=deep;
for(int i=;i<=deep;i++) ans[i]=path[i];
}
return;
}
for(int i=;i<g[x].size();i++)
{
int id=g[x][i];
if(f[id]==) continue;
dfs(e[id].v,deep+,sum+e[id].cost);
}
} void work()
{
int len=dis[][C2];
ans1=len; ans2=;
for(int i=;i<tot;i++)
if(dis[][e[i].u]+e[i].L+dis[][e[i].v]==len)
f[i]=;
cnt=; dfs(C1,,);
for(int i=;i<=cnt;i++)
{
printf("%d",ans[i]);
if(i<tot) printf(" ");
else printf("\n");
}
printf("%d ",ans1);
printf("%d\n",ans2);
} int main()
{
init();
read();
SPFA(,C1);
SPFA(,C2);
work();
return ;
}
PAT (Advanced Level) 1030. Travel Plan (30)的更多相关文章
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 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 ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- 1030 Travel Plan (30)(30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
随机推荐
- EF中用Newtonsoft.Json引发的循环引用问题
描述: 1.双向关系表a->b b->aList 2.在查询a引用b以后 3.用Newtonsoft.Json 去tojsonstring 4.一个只有6条数据的json串 出现了一屏幕字 ...
- MyEclipse运行到断点也跳过的问题
如果是B/S开发也就是javaWeb开发的话,Tomcat 的启动模式要设置成Debug模式 还有下面是没运行时断点的样子: 运行的时候,断点会变成对钩,表示执行到它所在代码的时候会停下来:
- .net简单的静态页生成
1.得到实体对象model,读取模板 string htmlMaster = File.ReadAllText(HttpContext.Current.Server.MapPath("/ma ...
- arm的编译器里已经有C标准库的lib包了,android为啥还要自己再实现呢
arm的编译器里已经有C标准库的lib包了,android为啥还要自己再实现呢 google自己搞的bionic libc来替代glibc想来是有原因的,本来glibc也是lgpl,应该也没有版权问题 ...
- on-tap和on-click
简单的说,当你点击移动设备屏幕上的一个点之后,on-tap会立刻触发,而on-click(可能)需要等待300ms才触发——这是移动设备浏览器为了检测是否存在双击的一个检测周期长度. 在移动设备上,应 ...
- linux sigaction信号处理
sigaction函数相比signal函数更为复杂,但更具灵活性,下面具体介绍她的结构和用法: #include <signal.h> int sigaction(int signum, ...
- 编程实现Windows瞬间关机
我们先来看看Windows正常的关机流程:①关机指令通知Windows子系统csrss.exe,csrss.exe收到通知后会和Winlogon.exe做一个数据交换,再由Winlogon.exe通知 ...
- linux学习的哲学层面的思考-架构
参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...
- Openlayers 3 的 imagelayer
<body> <div id="map"></div> <script> var extent = [0, 0, 1024, 968 ...
- IDL和生成代码分析
IDL:接口描述语言 这里使用thrift-0.8.0-xsb这个版本来介绍IDL的定义以及简单实例分析. 1. namespace 定义包名 2.struct 结构体,定义服务接口的参数和返回值用到 ...