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 ...
随机推荐
- 开始制作国产的 scratch
首先分析下API和数据格式: https://api.scratch.mit.edu/proxy/featured GET 23.235.37.162:443 返回数据与对应栏目: commun ...
- json解析尖括号<>
如题 rs.getString("HEADLINE").replaceAll("<", " <").replaceAll(&qu ...
- sqldeveloper连接mysql
sqldeveloper连接mysql 腑镜诏 钌d 汁叁ㄧ勋 佚蔡弥噙 不仅仅是越南帮和戴爷的事情还有谢婉莹的条 炔验遒其 倒扇油┣ 砹笄谤 几句话孟飞的信心大增兴奋不已换句话说 谠诂k厝 ...
- PHP扩展开发-简单类扩展
今天来学习简单类扩展开发 实现目标为如下php的类 <?php class classext(){ private $username; CONST URL="http://www.g ...
- CocoaAsyncSocket框架的简单封装
在iOS开发中使用socket(CFNetwork),一般都是用第三方库AsyncSocket. 参考博客:http://my.oschina.net/worldligang/blog/396881? ...
- SpringMVC redirect乱码问题
转:http://blog.csdn.net/xubo_zhang/article/details/8239725 spring redirect 用spring redirect中文会乱码:如下示例 ...
- 准备着手学习python
1. python 的框架 Tornado 网址: http://www.tornadoweb.org/en/stable/ github: https://github.com/tornadoweb ...
- Python定制类
https://docs.python.org/3/reference/datamodel.html#special-method-names
- 用 Swift 编写面向协议的网络请求
和我一起参加9 月 1 日 - 9月 2 日在纽约举办的 Swift 社区庆典
- Swift中的闭包(Closure) 浅析
转载自:http://www.devtalking.com/articles/closure-expressions-in-swift/ 闭包在Swift中非常有用.通俗的解释就是一个Int类型里存储 ...