dijkstra优先队列优化
优先队列里面的元素从小到大,假如1连着2,3,4,5.距离分别是9,8,7,6
所以优先队列里面是6,7,8,9.所以1到5最短距离就是6. 5出队列,假如5连着6,7,8
距离分别是1,2,3.所以5-6就是1.是最断的。
它优化的地方在于用过的点,直接在图中去掉,节省时间,因为是排好序的,每一次弹出来的都是最小的
typedef long long ll ;
const ll maxn = 150010 ;
const ll inf = 1e10 ;
using namespace std;
struct node
{
int v ;
ll cost ;
node(int x,ll y)
{
v = x ;
cost = y ;
}
};
struct root
{
int v ;
ll cost;
root(int x,ll y)
{
v = x ;
cost = y;
}
bool operator <(const root &x)const
{
return cost > x.cost;
}
};
vector<node>e[maxn];
int n, m, f,s, t ;
bool vis[maxn] ;
ll dis[maxn] ;
void add(int u, int v,int w)
{
e[u].push_back(node(v,w));
}
void disk()
{
priority_queue<root>que;
while(!que.empty())
que.pop();
memset(vis,0,sizeof(vis));
for(int i = 0 ; i< n ; i++)
dis[i] = inf ;
que.push(root(s,0));
dis[s] = 0;
while(!que.empty())
{
root x = que.top();
que.pop();
int u = x.v ;
if(vis[u])
continue;
vis[u] = 1;
for(int i = 0 ; i<e[u].size(); i++)
{
int v = e[u][i].v;
ll cost = e[u][i].cost;
if(!vis[v]&&dis[v] > dis[u] + cost)
{
dis[v] = dis[u] + cost;
que.push(root(v,dis[v]));
}
}
}
}
int main()
{
int u, v,w;
ios::sync_with_stdio(false);
cin >> n >>m >>f >>s >>t;
while(m--)
{
cin >> u >> v >> w;
add(u,v,w);
add(v,u,w);
}
ll mi = inf ;
disk();
mi = min(mi,dis[t]);
while(f--)
{
cin >> u >> v ;
add(u,v,0);
disk();
mi = min(mi,dis[t]);
e[u].erase(e[u].end() - 1);
}
printf("%lld\n",mi);
return 0;
}