题目链接:https://nanti.jisuanke.com/t/31001
L-Magical Girl Haze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256MB
Problem Description
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, M and K. Then the following M lines each line has three integers, describe a road, Ui, Vi, Ci. There might be multiple edges between u and v.
It is guaranteed that N<=100000, M<=200000, K<=10, 0<=c[i]<=1e9. There is at least one path between City 1 and City N.
Output
For each test case, print the minimum distance.
Sample Input
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2
Sample Output
3
题目大意:有n个城市,m条路,给出一个点到另一个点的距离,你有机会使最多K条边的距离变成0,问最短路
以前没写过这类题,刚开始写的时候一筹莫展,抱着绝望的心情到处google,还真被我发现了原题.....
原题是bzoj的2763,题目几乎是一样的,只有数据不同,但做的方法完全一样,就是个分层图最短路的模板题,但是用spfa写了一遍T了,改成dijkstra就过了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int,int> P;
const int maxn=1e5+7;
const int maxm=2e5+7;
const int inf=0x3f3f3f3f;
struct Node
{
int to;
int len;
int next;
}edge[maxm<<1];
int cnt;
int n,m,k;
int head[maxn];
int source,sink;
int dis[maxn][11];
bool vis[maxn][11];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
return;
}
void add(int u,int v,int len)
{
edge[cnt].to=v;
edge[cnt].len=len;
edge[cnt].next=head[u];
head[u]=cnt++;
return;
}
void dijkstra()
{
priority_queue<P,vector<P>,greater<P> > que;
que.push(make_pair(source,0));
memset(dis,inf,sizeof(dis));
dis[source][0]=0;
while(!que.empty())
{
P now=que.top();
que.pop();
int node=now.first;
int lev=now.second;
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v][lev]>dis[node][lev]+edge[i].len)
{
dis[v][lev]=dis[node][lev]+edge[i].len;
que.push(make_pair(v,lev));
}
if(lev+1<=k)
{
if(dis[v][lev+1]>dis[node][lev])
{
dis[v][lev+1]=dis[node ][lev];
que.push(make_pair(v,lev+1));
}
}
}
}
return;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int test;
scanf("%d",&test);
while(test--)
{
init();
scanf("%d%d%d",&n,&m,&k);
source=1;
sink=n;
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
dijkstra();
printf("%d\n",dis[sink][k]);
}
return 0;
}