HDU 1595 find the longest of the shortest(Dijkstra)
http://acm.hdu.edu.cn/showproblem.php?pid=1595
题意:有一个N个点M条边的无向图,现在要从1号点走到N号点去.但是该图中有一条边(不知道是哪条)不能走了,问你从1号点到N号点最多要花多少时间.(保证就算删除一条边,从1号点到N号点依然是通路的)
分析:
首先原图存在一条从1号点到N号点的最短路径.如果这条坏边不在该最短路径上,那么花的时间肯定是最小不变的.
所以对于不在最短路径上的边,我们可以直接不考虑了.
首先我们求出原图从1号点到N号点的最短距离,且把该最短路径上的边都标记好. 然后我们依次使得每条被标记的边不能行走,然后求对应的1号点到N号点的最短路径. 保存最大值即可.
源程序中有个小问题.对于一条边(a,b).我们原先添加的是两条边:a->b和b->a.但是我们之后计算从0到n-1的最短路径时,我们只限制了单向的边(可能是a->b边)不能走.这样会不会出问题的呢?
可以证明,我们限制了a->b边就等于是限制了a与b之间的无向边,因为从0到n-1的单向最短路径肯定不经过b->a.所以我们不限制b->a也没关系.
当然这里我们可以直接同时限制a->b和b->a的边.因为这两条边的序号必然是如下关系:
(假设他们序号为x和y) x=y^1
且y^1=x.(想想为什么)
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn = 1000+10;
struct Edge
{
int from,to,dist;
Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct HeapNode
{
int d,u;
HeapNode(int d,int u):d(d),u(u){}
bool operator <(const HeapNode&rhs)const
{
return d>rhs.d;
}
};
struct Dijkstra
{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
int p[maxn];
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back(Edge(from,to,dist));
m = edges.size();
G[from].push_back(m-1);
}
int dijkstra(int edge_id) //egde_id 是被限制不能走的边
{
priority_queue<HeapNode> Q;
for(int i=0;i<n;i++) d[i]=INF;
d[0]=0;
Q.push(HeapNode(d[0],0));
memset(done,0,sizeof(done));
while(!Q.empty())
{
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();i++)if(G[u][i]!=edge_id)
{
Edge &e=edges[G[u][i]];
if(d[e.to] > d[u]+e.dist)
{
d[e.to] = d[u]+e.dist;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to],e.to));
}
}
}
return d[n-1];
}
}DJ;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2&&n)
{
DJ.init(n);
for(int i=0;i<m;i++)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
u--,v--;
DJ.AddEdge(u,v,d);
DJ.AddEdge(v,u,d);
}
int ans = DJ.dijkstra(m*2+1); //原图最短距离
vector<int> edge_id; //记录从0到n-1最短路径上的所有边
int e=n-1; //当前终点
while(e!=0) //从终点逆向推导路径
{
edge_id.push_back(DJ.p[e]);
e = DJ.edges[DJ.p[e]].from;
}
for(int i=0;i<edge_id.size();i++)
ans = max(ans,DJ.dijkstra(edge_id[i]));
printf("%d\n",ans);
}
return 0;
}
分享到:
相关推荐
Please write a program to find the smallest positive integer n that (f(n,m)?n)⊕n=k, or determine it is impossible. Input The first line of the input contains an integer T(1≤T≤10), denoting the ...
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
NULL 博文链接:https://128kj.iteye.com/blog/1716470
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
« ACM模板收集Let the ...4.the number of paths of length 2n through an n-by-n grid that do not rise above the main diagonal. 在这里记下一个重要的结论,一个生成树的有n各节点 可以用 n^(n-2)中生成树.
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦