单源最短路
定义一个cost数组记录边的权值,然后删边(改值为INF)跑最短路就行了
第一遍跑的时候,别忘了记录一下每个点的父节点,方便删边
代码如下(dijkstra):
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define maxn 2003
#define INF 1000000007
using namespace std;
typedef long long LL;
LL n,m,tot,ans = 0;
LL fa[maxn];
LL head[maxn];
LL dist[maxn];
LL cost[maxn][maxn];
bool used[maxn];
struct data
{
LL f,t,c,next;
}es[(maxn*(maxn >> 1)) << 1];
struct node
{
LL u,v;
bool operator < (const node &x)const
{
return v > x.v;
}
};
inline void build(LL x,LL y,LL z)
{
tot++;
es[tot].f = x;
es[tot].t = y;
es[tot].c = z;
es[tot].next = head[x];
head[x] = tot;
}
priority_queue<node >q;
LL dij(LL x,LL y,bool pd)
{
memset(dist,68,sizeof(dist));
memset(used,0,sizeof(used));
q.push((node){x,0});
dist[x] = 0;
while(!q.empty())
{
node u = q.top();
q.pop();
LL now = u.u;
if(used[now] == true) continue;
used[now] = true;
for(LL i = head[now];i;i = es[i].next)
{
LL v = es[i].t;
if(dist[v] > dist[now]+cost[now][v])
{
dist[v] = dist[now]+cost[now][v];
if(pd) fa[v] = now;
// cout<<"dist of "<<v<<" : "<<dist[v]<<endl;
q.push((node){v,dist[v]});
}
}
}
// cout<<"dist of n: "<<dist[n]<<endl;
return dist[n];
}
inline void rd(LL &x)
{
scanf("%lld",&x);
}
inline void work()
{
for(LL i = 1;i <= m;i++)
{
LL a,b,c;
rd(a);rd(b);rd(c);
build(a,b,c);
build(b,a,c);
cost[a][b] = c;
cost[b][a] = c;
}
dij(1,n,1);
LL s = 1,e = n;
for(LL i = e;i != s;i = fa[i])
{
LL v = fa[i];
LL x = cost[i][v];
cost[i][v] = cost[v][i] = INF;
ans = max(ans,dij(1,n,0));
cost[i][v] = cost[v][i] = x;
// cout<<ans<<endl;
}
printf("%lld\n",ans);
}
int main()
{
memset(dist,68,sizeof(dist));
memset(used,0,sizeof(used));
rd(n);rd(m);
work();
return 0;
}
THE END
By Peacefuldoge