先处理出最短路上的边。变成一个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)的更多相关文章

  1. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  2. 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 ...

  3. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  4. 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 ...

  5. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  6. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

  7. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  8. 1030 Travel Plan (30)(30 分)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

  9. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

随机推荐

  1. Redis和Memcached的区别【转】

    如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储.2 Redis支持数据 ...

  2. js连续赋值、指针

    jq的源码中有很多连续赋值,类似这样的: var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> unde ...

  3. sql查询百分号的方法

    select * from [tablename] where [col] like '%100/%%' escape '/'

  4. Sanatorium

    Sanatorium time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. java中io对文件操作的简单介绍

    11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...

  6. js 获取当前点击的标签

  7. AutoTile 自动拼接(四) 学习与实践

    今天主要来说下,数据绑定. 之前第一章,我说到 把 资源图 画成格子,你们应该还有印象吧. 那么,当我 知道 格子数据,能否拿到 资源对应的图片呢? 大家先复习一下 第一章,发现很多格子数据 是相同的 ...

  8. UVALive 2521 Game Prediction 题解

    这个我上来把题目理解错了,我以为所有人的牌都是一样的,感觉这个题太麻烦了吧,而且题目样例过不去啊……后来发现理解错了,给出的数据是他一个人的数据,就是让我们求他一定能赢的轮数,所有的牌是固定的(1 - ...

  9. Struts的前世今身

    1.Struts1的运行原理 a.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件( ...

  10. Windows恢复Grub引导,用grub安装ubuntu

    http://www.linuxidc.com/wap.aspx?nid=18027&p=&cp=&cid=http://m.blog.chinaunix.net/uid-22 ...