BZOJ1579: [Usaco2009 Feb]Revamping Trails 道路升级

1579: [Usaco2009 Feb]Revamping Trails 道路升级

Time Limit: 10 Sec   Memory Limit: 64 MB
Submit: 1936   Solved: 536
[ Submit][ Status][ Discuss]

Description

每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i (1 <= P1_i <= N; 1 <= P2_i<= N). John需要T_i (1 <= T_i <= 1,000,000)时间单位用道路i从P1_i走到P2_i或者从P2_i 走到P1_i 他想更新一些路经来减少每天花在路上的时间.具体地说,他想更新K (1 <= K <= 20)条路经,将它们所须时间减为0.帮助FJ选择哪些路经需要更新使得从1到N的时间尽量少.

Input

* 第一行: 三个空格分开的数: N, M, 和 K * 第2..M+1行: 第i+1行有三个空格分开的数:P1_i, P2_i, 和 T_i

Output

* 第一行: 更新最多K条路经后的最短路经长度.

Sample Input

4 4 1
1 2 10
2 4 10
1 3 1
3 4 100

Sample Output

1

HINT

K是1; 更新道路3->4使得从3到4的时间由100减少到0. 最新最短路经是1->3->4,总用时为1单位. N<=10000

Source

Gold


题解: 这题我写了好长时间 一个多半小时吧 (我好菜啊.jpg ) 分层图最短路 可以dp考虑一下

跟我以前写的分层图的方式不太一样 不过思想差不多 就是(我理解为hash一下建多平面图)比如乘个n什么的

听说这到题卡spfa 还是乖乖写dijkstra+优先队列吧,不能说很难 但我好!菜!啊! 

#include<bits/stdc++.h>
#define ll long long
#define N 10005
#define M 100005
const int INF = 0x7fffffff;
const double eps = 1e-5;
const int maxn = 50000 + 5; 
using namespace std;
int n,m,K,cnt,k;
int to[M],head[N],next[M],w[M];
int dis[N][25],vis[N][25];

void Add(int u,int y,int z)
{
	to[++cnt] = y; next[cnt] = head[u];head[u] = cnt;w[cnt] =z;
	to[++cnt] = u; next[cnt] = head[y];head[y] = cnt;w[cnt] =z;
}
int read()
{
	int x = 0 , f = 1; char ch = getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f*=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();}
	return x * f;
}
void dijkstra()
{
	priority_queue <pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
	memset(dis,127/3,sizeof(dis));
	dis[1][0] = 0;
	q.push(make_pair(0,1));
	while(!q.empty())
	{
		int now = q.top().second; q.pop();
		int z = now / (n + 1); now = now % (n + 1);
		if(vis[now][z]) continue;
		vis[now][z] = 1;
		for(int i = head[now] , y ; i ; i = next[i])
		{
			if(dis[y = to[i]][z] > dis[now][z] + w[i])
			{
				dis[to[i]][z] = dis[now][z] + w[i];
				q.push(make_pair(dis[to[i]][z],z*(n+1)+y));
			}
			if( z == k) continue; //跳跃次数用光了 
			if(dis[to[i]][z+1]>dis[now][z])
			{
				dis[to[i]][z+1]=dis[now][z];
				q.push(make_pair(dis[to[i]][z+1],(z+1)*(n+1)+y));
			}
		}
	}
}
int main()
{
	n = read(), m = read(), k = read();
	for(int i = 1 ; i <= m ; i++)
	{
		int x = read(),y = read(),z = read();
		Add(x,y,z);
	}
	dijkstra();
	printf("%d",dis[n][k]);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值