刷题—— hdu 6181 Two Paths
2017-08-24 20:01
323 查看
/*
裸的次短路
不断更新v->u的次短路,直到v->u的次短路只比最短路小
*/
#include <vector>
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
#define ll long long
#define INF 1e18
#define MAXM 100100
struct edge{int to;ll w;};
typedef pair<ll,int>P;
int n,r;
ll d[MAXM];
ll d1[MAXM];
vector<edge>G[MAXM];
bool vis[MAXM];
void solve(){
priority_queue<P, vector<P>,greater<P> >que;
fill(d+1,d+n+1,INF);
fill(d1+1,d1+n+1,INF);
d[1]=0;
que.push(P(0,1));
while(!que.empty(
4000
)){
P p=que.top();
que.pop();
int v=p.second;
if(d1[v]<p.first)continue;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
ll d2=p.first+e.w;
if(d[e.to]>d2){
swap(d[e.to],d2);
que.push(P(d[e.to],e.to));
}
if(d1[e.to]>d2&&d[e.to]<d2){
d1[e.to]=d2;
que.push(P(d1[e.to],e.to));
}
}
}
printf("%I64d\n",d1
);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&r);
for(int i=1;i<=n;i++){
G[i].clear();
}
int u,v;
ll w;
while(r--){
scanf("%d %d %I64d",&u,&v,&w);
G[u].push_back(edge{v,w});
G[v].push_back(edge{u,w});
}
solve();
}
return 0;
}
裸的次短路
不断更新v->u的次短路,直到v->u的次短路只比最短路小
*/
#include <vector>
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
#define ll long long
#define INF 1e18
#define MAXM 100100
struct edge{int to;ll w;};
typedef pair<ll,int>P;
int n,r;
ll d[MAXM];
ll d1[MAXM];
vector<edge>G[MAXM];
bool vis[MAXM];
void solve(){
priority_queue<P, vector<P>,greater<P> >que;
fill(d+1,d+n+1,INF);
fill(d1+1,d1+n+1,INF);
d[1]=0;
que.push(P(0,1));
while(!que.empty(
4000
)){
P p=que.top();
que.pop();
int v=p.second;
if(d1[v]<p.first)continue;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
ll d2=p.first+e.w;
if(d[e.to]>d2){
swap(d[e.to],d2);
que.push(P(d[e.to],e.to));
}
if(d1[e.to]>d2&&d[e.to]<d2){
d1[e.to]=d2;
que.push(P(d1[e.to],e.to));
}
}
}
printf("%I64d\n",d1
);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&r);
for(int i=1;i<=n;i++){
G[i].clear();
}
int u,v;
ll w;
while(r--){
scanf("%d %d %I64d",&u,&v,&w);
G[u].push_back(edge{v,w});
G[v].push_back(edge{u,w});
}
solve();
}
return 0;
}
相关文章推荐
- Hdu 6181 Two Paths【次短路】
- HDU - 6181 Two Paths(真 · 求次短路径)
- hdu 6181 Two Paths(次短路径长度)POJ 3255 Roadblocks ( 次短路长度)
- 【hdu 6181】Two Paths
- HDU --- 6181 Two Paths 2017第十场多校签到题【裸次短路】
- HDU 6181 Two Paths (次短路)
- HDU 6181 Two Paths
- 2017 第十场多校训练 HDU 6181 Two Paths 次短路+Dijkstra
- HDU 6181 Two Paths(次短路变形)
- HDU 6181 Two Paths 次短路
- hdu 6181 Two Paths (次短路)
- hdu 6181 Two Paths (次短路)
- hdu 6181 Two Paths
- hdu 6181 Two Paths -最短路条数+次短路 - 多校联盟10
- HDU 6181 Two Paths
- HDU 6181 Two Paths【次短路】【模板题】
- [hdu 6181 Two Paths] Dijkstra求次短路
- hdu 6181 Two Paths
- hdu 6181 Two Paths(次短路)
- 2017多校第10场 HDU 6181 Two Paths 次短路