Description
Farmer John dutifully checks on the cows every day. He traverses so me of the M (1 <= M <= 50,000) trails conveniently numbered 1..M from pasture 1 all the way out to pasture N (a journey which is always possible for trail maps given in the test data). The N (1 <= N <= 10,000) pastures conveniently numbered 1..N on Farmer John’s farm are currently connected by bidirectional dirt trails. Each trail i connects pastures P1_i and P2_i (1 <= P1_i <= N; 1 <= P2_i <= N) and requires T_i (1 <= T_i <= 1,000,000) units of time to traverse.
He wants to revamp some of the trails on his farm to save time on his long journey. Specifically, he will choose K (1 <= K <= 20) trails to turn into highways, which will effectively reduce the trail’s traversal time to 0. Help FJ decide which trails to revamp to minimize the resulting time of getting from pasture 1 to N.
Input
Line 1: Three space-separated integers: N, M, and K
Lines 2..M+1: Line i+1 describes trail i with three space-separated
integers: P1_i, P2_i, and T_i
Output
- Line 1: The length of the shortest path after revamping no more than
K edges
Sample Input
4 4 1
1 2 10
2 4 10
1 3 1
3 4 100
Sample Output
1
Key To Problem
小小的图论+二维dp
dis[u][num]表示到点u,翻新num条道路所需要走的最短的时间
dis[to][num]=dis[u][num]+s;
dis[to][num+1]=dis[u][num];
需要用dijkstra+堆优化,据说SPFA超时,并没有尝试。
CODE
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10010
using namespace std;
vector<int>V[N];
vector<int>pre[N];
struct node
{
int u,num,s;
bool friend operator <(node x,node y)
{
return x.s>y.s;
}
};
int n,m,l;
int dis[N][30];
bool used[N][30];
void dijkstra(int u)
{
memset(dis,0x3f,sizeof(dis));
priority_queue<node>Q;
node k;
k.u=u,k.s=0,k.num=0;
Q.push(k);
dis[u][0]=0;
while(Q.size())
{
k=Q.top();
Q.pop();
if(used[k.u][k.num])
continue;
used[k.u][k.num]=1;
int num=k.num;
for(int i=0;i<V[k.u].size();i++)
{
int to=V[k.u][i];
int s=pre[k.u][i];
if(dis[to][num]>dis[k.u][num]+s)
{
dis[to][num]=dis[k.u][num]+s;
node p;
p.u=to,p.num=num,p.s=dis[to][num];
Q.push(p);
}
if(num+1<=l&&dis[to][num+1]>dis[k.u][num])
{
dis[to][num+1]=dis[k.u][num];
node p;
p.u=to,p.num=num+1,p.s=dis[to][num+1];
Q.push(p);
}
}
}
}
int main()
{
// freopen("revamp.in","r",stdin);
// freopen("revamp.out","w",stdout);
scanf("%d%d%d",&n,&m,&l);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
V[x].push_back(y);
pre[x].push_back(z);
V[y].push_back(x);
pre[y].push_back(z);
}
dijkstra(1);
printf("%d\n",dis[n][l]);
return 0;
}

农夫约翰每天都要检查奶牛,他需要穿越M条双向小径从草地1到草地N。现在他计划将最多K条小径升级为高速公路,以缩短行程时间。给定每条小径的起始草地、结束草地和时间,求升级最少数量的小径后,从草地1到N的最短路径时间。
545

被折叠的 条评论
为什么被折叠?



