zoukankan      html  css  js  c++  java
  • LuoguP2939改造路

    点此跳转

    题目描述

    Farmer John dutifully checks on the cows every day. He traverses some 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.

    题意翻译

    约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体.
    通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路.在高速公路上的通行几乎是瞬间完成的,所以高速公路的通行时间为0.
    请帮助约翰决定对哪些小径进行升级,使他每天从1号牧场到第N号牧场所花的时间最短

    TIME LIMIT: 2 seconds

    【输入输出格式】

    输入格式:

    • 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

    输出格式:

    • Line 1: The length of the shortest path after revamping no more than K edges

    【输入样例#1】:
    4 4 1
    1 2 10
    2 4 10
    1 3 1
    3 4 100
    【输出样例#1】:
    1
    说明
    K is 1; revamp trail 3->4 to take time 0 instead of 100. The new shortest path is 1->3->4, total traversal time now 1.

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn = 5000001;
    inline int read(){
    	int s,f = 1;
    	char ch;
    	ch = getchar();
    	while(!isdigit(ch)){
    		if(ch == '-') f = -1;
    		ch = getchar();
    	}
    	while(isdigit(ch)){
    		s = s*10 + ch -'0';
    		ch = getchar();
    	}
    	return s*f;
    }
    struct edge{
    	int to,next,v;
    }e[maxn];
    int head[maxn],dis[maxn],n,m,num_edge,k;
    bool vis[maxn];
    void Dijkstra(int s){
    	priority_queue<pair<int,int> > q;
    	q.push(make_pair(0,s));
    	memset(dis,0x3f,sizeof(dis));
    	memset(vis,0,sizeof(vis));
    	dis[s] = 0;
    	while(!q.empty()){
    		int u = q.top().second;
    		q.pop();
    		if(vis[u]) continue;
    		vis[u] = 1;
    		for(int i = head[u];i;i = e[i].next){
    			if(dis[u] + e[i].v < dis[e[i].to]){
    				dis[e[i].to] = dis[u] + e[i].v;
    				q.push(make_pair(-dis[e[i].to],e[i].to)); //小根堆
    			}
    		}
    	}
    }
    void add(int from,int to,int v){
    	e[++num_edge].next = head[from];
    	e[num_edge].to = to;
    	e[num_edge].v = v;
    	head[from] = num_edge;
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin>>n>>m>>k;
    	for(int i = 1;i <= m;i++){
    		int a,b,c;
    		cin>>a>>b>>c;
    		add(a,b,c);
    		add(b,a,c);
    		for(int j = 1;j <= k;j++){
    			add(j*n + a,j*n + b,c);
    			add(j*n + b,j*n + a,c);
    			if(j){
    				add((j - 1)*n + a,j*n + b,0);
    				add((j - 1)*n + b,j*n + a,0);
    			}
    		}
    	}
    	Dijkstra(1);
    	int ans = dis[n];
    	for(int i = 1;i <= k;i++) ans = min(ans,dis[n + n*i]);
    	printf("%d",ans);
    	return 0;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    入职事业单位一年,真真的发现与私企大不同
    就算收入腰斩,我也决定离开私企,去吃公家饭了
    写了8年的代码,做过的项目都下线了,程序员的意义在哪里!
    程序员又双叕和产品打起来了,说说与产品经理的那些事。
    创业公司的技术总监,去上市公司面试,结果凉了。
    看了程序员的未来在哪里,有感
    有个程序猿要去当CEO了:(一)事情始末
    企业微信/政务微信 previewFile方法在有iframe情况下且iOS点击无效解决方案
    Mark: 实现个toy版的脚手架(RPC)
    搞技术的思维
  • 原文地址:https://www.cnblogs.com/Zforw/p/10370337.html
Copyright © 2011-2022 走看看