好厉害呀这道题,有种豁然开朗的感觉。。。。

按拓扑顺序跑最短路。

然后注意细节,像WA的代码犯下的错是一笔带过没有丝毫考虑的。。。然而就是错了。

考试的时候一定要拍啊。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define maxv 25050
#define maxe 200050
#define inf 1000000000
using namespace std;
struct status
{
int len,v;
bool operator < (const status &x) const
{
return len>x.len;
}
status (int v,int len):v(v),len(len){}
status () {}
};
int n,m1,m2,s,dis[maxv],d[maxv],father[maxv],g[maxv],nume=,tot=,x,y,z,bel[maxv];
vector <int> v[maxv],fr[maxv],to[maxv],w[maxv];
vector <status> bl[maxv];
map <int,int> mp;
queue <int> qr;
priority_queue <status> q;
struct edge
{
int v,w,nxt;
}e[maxe];
void addedge(int u,int v,int w)
{
e[++nume].v=v;
e[nume].w=w;
e[nume].nxt=g[u];
g[u]=nume;
}
int getfather(int x)
{
if (father[x]!=x) father[x]=getfather(father[x]);
return father[x];
}
void unionn(int x,int y)
{
int f1=getfather(x),f2=getfather(y);
if (f1==f2) return;
father[f2]=f1;
}
void dijkstra(int x)
{
for (int i=;i<bl[x].size();i++) dis[bl[x][i].v]=min(dis[bl[x][i].v],bl[x][i].len);
for (int i=;i<v[x].size();i++) if (dis[v[x][i]]!=inf) q.push(status(v[x][i],dis[v[x][i]]));
while (!q.empty())
{
status head=q.top();q.pop();
for (int i=g[head.v];i;i=e[i].nxt)
{
int v=e[i].v;
if (dis[v]>dis[head.v]+e[i].w)
{
dis[v]=dis[head.v]+e[i].w;
q.push(status(v,dis[v]));
}
}
}
}
void topusort()
{
for (int i=;i<=tot;i++)
{
if (!d[i]) qr.push(i);
bl[i].push_back(status(v[i][],inf));
}
while (!qr.empty())
{
int head=qr.front();qr.pop();
if (head==bel[s]) bl[bel[s]].push_back(status(s,));
dijkstra(head);
for (int i=;i<fr[head].size();i++)
{
if (dis[fr[head][i]]!=inf)
{
status now=status(to[head][i],dis[fr[head][i]]+w[head][i]);
bl[bel[to[head][i]]].push_back(now);
}
if (!--d[bel[to[head][i]]]) qr.push(bel[to[head][i]]);
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m1,&m2,&s);
for (int i=;i<=n;i++) {father[i]=i;dis[i]=inf;}
for (int i=;i<=m1;i++)
{
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z);addedge(y,x,z);
unionn(x,y);
}
for (int i=;i<=n;i++)
{
int f=getfather(i);
if (!mp[f]) mp[f]=++tot;
bel[i]=mp[f];v[mp[f]].push_back(i);
}
for (int i=;i<=m2;i++)
{
scanf("%d%d%d",&x,&y,&z);
fr[bel[x]].push_back(x);to[bel[x]].push_back(y);w[bel[x]].push_back(z);
d[bel[y]]++;
}
topusort();
for (int i=;i<=n;i++)
{
if (dis[i]==inf) printf("NO PATH\n");
else printf("%d\n",dis[i]);
}
return ;
}

BZOJ 2200 道路与航线的更多相关文章

  1. BZOJ 2200 道路与航线(图论)

    BZOJ 2200 道路与航线 题目大意 有两种路,一种没负数,一种没环,求单源最短路. solution 存在负边权Dij一定不能用嘛,显然不是 根据题意能看出来是tarjan,将双向边缩点,得到的 ...

  2. BZOJ 2200 道路与航线 (算竞进阶习题)

    dijkstra + 拓扑排序 这道题有负权边,但是卡了spfa,所以我们应该观察题目性质. 负权边一定是单向的,且不构成环,那么我们考虑先将正权边连上.然后dfs一次找到所有正权边构成的联通块,将他 ...

  3. 【BZOJ】【2200】【USACO 2011 Jan】道路和航线

    做了一天…… TLE:数组开小了-_-#道路是有50000的,双向要乘二.(我特么怎么想的就以为是树了……) WA:一些大点都WA了,小点都过了.好纠结…… AC了QAQ,不知道为什么,在并查集合并的 ...

  4. [BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  5. bzoj 2200: [Usaco2011 Jan]道路和航线——拓扑+dijkstra

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  6. BZOJ 2200: [Usaco2011 Jan]道路和航线

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  7. BZOJ 2200--[Usaco2011 Jan]道路和航线(最短路&拓扑排序)

    2200: [Usaco2011 Jan]道路和航线 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1128  Solved: 414[Submit] ...

  8. 2200: [Usaco2011 Jan]道路和航线 (拓扑排序+dijstra)

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  9. BZOJ2200 道路和航线【好题】【dfs】【最短路】【缩点】

    2200: [Usaco2011 Jan]道路和航线 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1384  Solved: 508[Submit] ...

随机推荐

  1. hdu----(5050)Divided Land(二进制求最大公约数)

    Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. hdu----(3118)Arbiter(构造二分图)

    Arbiter Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  3. Remove Duplicates from Sorted List II [LeetCode]

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. SQL Sever 2008 安装

    http://jingyan.baidu.com/article/4b07be3c1daf1248b380f33b.html 大致出错信息如下:RebootRequiredCheck 检查是否需要挂起 ...

  5. 218. The Skyline Problem *HARD* -- 矩形重叠

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  6. 关于 iOS 批量打包的总结

      关于 iOS 批量打包的总结 本文作者: 伯乐在线 - Tsui YuenHong .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 如果你曾经试过做多 target 的项目,到了测试人员要 ...

  7. hadoop主节点(NameNode)备份策略以及恢复方法

    link:http://jiajun.iteye.com/blog/809125 一.dits和fsimage 首先要提到两个文件edits和fsimage,下面来说说他们是做什么的. 集群中的名称节 ...

  8. 各式各样table

    <html> <head>      <title>大清帝国</title>      <meta name="keywords&quo ...

  9. SecureCRT从本传相片到服务器的注意事项

    rz -y -be 注意,要加上参数be

  10. Go语言并发与并行学习笔记(三)

    转:http://blog.csdn.net/kjfcpua/article/details/18265475 Go语言并发的设计模式和应用场景 以下设计模式和应用场景来自Google IO上的关于G ...