L. Magical Girl Haze
- 262144K
There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a distance ci. Haze is a Magical Girl that lives in City 1, she can choose no more than K roads and make their distances become 0. Now she wants to go to City N, please help her calculate the minimum distance.
Input
The first line has one integer T(1≤T≤5), then following T cases.
For each test case, the first line has three integers N, Mand K.
Then the following M lines each line has three integers, describe a road, Ui,Vi,Ci. There might be multiple edges between uu and v.
It is guaranteed that N≤100000,M≤200000,K≤10,0≤Ci≤1e9. There is at least one path between City 1 and City N.
Output
For each test case, print the minimum distance.
样例输入
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2
样例输出
3
拆点建立分层图
建边时相当于每个边扩建2k倍,最开始出现bug是因为建边时都写成了for(i=1;i<=k;i++),但是i=0要建,这是最初的边
然后后面的是for(i=0;i<k;i++)因为接下来相当于每个点想下一个点的v连了一条费用为0的边
一共有n*k+n个点
1->5 w
11->15 w
21->25 w
1->15 0
11->25 0
建完图后跑最短路就可以了
!!!!记得数组要开大!开大!开大!wa到哭
计蒜客只说是错误
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e6+5;
const int maxm=5e6+5;
const ll inf =1e18;
int head[maxm],nx[maxm],from[maxm],to[maxm];
int e;
ll cost[maxm];
struct node
{
int id;
ll pay;
}dis[maxm];
bool operator<(node a,node b)
{
return a.pay>b.pay;
}
priority_queue <node> q;
int n,m,k;
void add(int u,int v,ll w)
{
for(int i=0;i<=k;i++)
{
e++;
from[e]=u+i*n;
to[e]=v+i*n;
cost[e]=w;
nx[e]=head[u+i*n];
head[u+i*n]=e;
}
for(int i=0;i<k;i++)
{
e++;
from[e]=u+i*n;
to[e]=v+(i+1)*n;
cost[e]=0;
nx[e]=head[u+i*n];
head[u+i*n]=e;
}
}
void djstla(int x)
{
dis[x].pay=0;
q.push(dis[x]);
while(!q.empty())
{
node u=q.top();
q.pop();
for(int i=head[u.id];i!=-1;i=nx[i])
{
int v=to[i];
if(dis[v].pay>dis[u.id].pay+cost[i])
dis[v].pay=dis[u.id].pay+cost[i],q.push(dis[v]);
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
memset(head,-1,sizeof(head));
e=0;
for(int i=1;i<maxm;i++)
{
dis[i].id=i,dis[i].pay=inf;
}
while(m--)
{
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
}
djstla(1);
printf("%lld\n",dis[k*n+n].pay);
}
return 0;
}