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 ...
随机推荐
- 微信支付坑:url未注册
微信支付,报:url未注册 页面URL未注册 支付授权目录 这里很重要我就是在这里折腾了很久.怎么设置呢,首先要看你支付的当前页面URL 比如是:http://www.taidupa.com/wxpa ...
- WEBROOT根目录 <%=request.getContextPath()%>
WEBROOT根目录 <%=request.getContextPath()%> == ${pageContext.request.contextPath}
- JSP基本语法--实例演练
基本语法概括:<%@page>,<%@include>,<jsp:include>,<jsp:forward> 加上数据库操作,可以开发动态web了. ...
- android之DPAD上下左右四个键控制
我们代码的目的很简单,那就是监听上下左右中这几个键的事件触发.直接上代码: dpad.xml <?xml version="1.0" encoding="utf-8 ...
- Redis如何保存数组和对象
个人建议使用PHP自带的序列化函数serialize和unserialize函数 我们可以封装一个自己的Redis类 <?php class MyRedis{ private static $h ...
- 笨方法学python--简介
该章提到编程新手所需的三种最重要的技术:读和写,注重细节,发现不同. 读 和 写 即熟悉代码中的各种字符. 注 重 细 节 将例子一字不差地打出来,通过实践训练自己 发 现 不 同 这个是通过长年累月 ...
- 倒计时demo
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (strong,nonatom ...
- SpringMVC redirect乱码问题
转:http://blog.csdn.net/xubo_zhang/article/details/8239725 spring redirect 用spring redirect中文会乱码:如下示例 ...
- idea编译报错:未结束的字符串文字;非法的表达式;未结束的字符串字面值
在idea的Settings中,找到File Encodings,将IDE Encoding 改为UTF-8 要多试几次,清除缓存什么的,具体原因不知道,不过经常第一次修改不能成功.
- Oracle中清除BIN$开头的垃圾表的解决办法
10g的新特性flashback闪回区 在10g中bin开头表示已经删除的放在回收站的表,oracle在删除表时并没有彻底的删除,而是把表放入回收站!purge recyclebin清空回收站即可. ...