您的位置:首页 > 大数据 > 人工智能

USACO 2009 Feb Gold 3.Revamping Trails

2015-08-21 18:33 686 查看

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
;
vector<int>pre
;
struct node
{
int u,num,s;
bool friend operator <(node x,node y)
{
return x.s>y.s;
}
};
int n,m,l;
int dis
[30];
bool used
[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
[l]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图论 USACO