样例输入
4 7
1 2 2
2 3 2
1 3 4
4 1 2
4 2 2
3 4 1
4 3 5
样例输出
1 7
从n出发 正反建边跑两次dij
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
int n,m;
const int MAXE = 1e5+10;
const int MAXV = 1e3+10;
int head[MAXE], da[MAXV], db[MAXV];
int x[MAXE],y[MAXE],z[MAXE];
int tal;
struct eg {
int nxt,to,cost;
}edge[MAXE];
void init(){
tal = 1;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cost){
edge[tal] = {head[u],v,cost};
head[u] = tal++;
}
typedef pair<int ,int > P;
priority_queue<P ,vector<P>, greater<P> > q;
void dija(){
memset(da,0x3f,sizeof(da));
while (!q.empty()) q.pop();
q.push(P(0,n));
da[n] = 0;
while (!q.empty()){
P p = q.top(); q.pop();
int dis = p.first,v = p.second;
if (da[v]<dis) continue;
for (int i = head[v];i;i=edge[i].nxt){
eg &e = edge[i];
if (da[v]+e.cost<da[e.to]){
da[e.to] = da[v] + e.cost;
q.push(P(da[e.to],e.to));
}
}
}
}
void dijb(){
memset(db,0x3f,sizeof(db));
while (!q.empty()) q.pop();
q.push(P(0,n));
db[n] = 0;
while (!q.empty()){
P p = q.top(); q.pop();
int dis = p.first,v = p.second;
if (db[v]<dis) continue;
for (int i = head[v];i;i=edge[i].nxt){
eg &e = edge[i];
if (db[v]+e.cost<db[e.to]){
db[e.to] = db[v] + e.cost;
q.push(P(db[e.to],e.to));
}
}
}
}
int main(){
while (scanf("%d%d",&n,&m)!=EOF){
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(z,0,sizeof(z));
init();
for (int i = 0;i < m; ++i){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
addedge(x[i],y[i],z[i]);
}
dija();
init();
for (int i = 0;i < m; ++i){
addedge(y[i],x[i],z[i]);
}
dijb();
int mx = -1;
int ft;
for (int i = 1;i <= n-1; ++i){
if (da[i]+db[i]>mx&&da[i]!=INF&&db[i]!=INF){
mx = da[i]+db[i];
ft = i;
}
}
printf("%d %d\n",ft,mx);
}
return 0;
}