• HDU4109-instruction agreement(差分约束-最长路+建立源点,汇点)


    Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW. 
    If the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance. 
    The definition of the distance between two instructions is the difference between their beginning times. 
    Now we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction. 
    Your job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time.

    Input

    The input consists several testcases. 
    The first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations. 
    The following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N - 1. 

    Output

    Print one integer, the minimum time the CPU needs to run. 

    Sample Input

    5 2
    1 2 1
    3 4 1

    Sample Output

    2
    
            
      

    Hint

    In the 1st ns, instruction 0, 1 and 3 are executed;
    In the 2nd ns, instruction 2 and 4 are executed.
    So the answer should be 2.
    题解:
     加一个源点s指向所有点边权为0,
     加一个汇点t,所有点指向t边权为0,
     u和v安全距离为w,则加边v->u,边权为-w
     (因为 u-v>=w 所以v-u<=-w )
     求s到t最短路即可。

    参考代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    const int inf = 0x3f3f3f3f;
    const int maxn = 1005;
    const int maxm = 11005;
    int n, m;
    struct node
    {
    	int v, w, next;
    }edge[maxm];
    int no, head[maxn];
    int vis[maxn], dis[maxn], cnt[maxn];
    queue<int> q;
    inline void init()
    {
    	no = 0;
    	memset(head, -1, sizeof head);
    }
    inline void add(int u, int v, int w)
    {
    	edge[no].v = v; edge[no].w = w;
    	edge[no].next = head[u]; head[u] = no++;
    }
    void SPFA()
    {
    	while(!q.empty()) q.pop();
    	memset(vis, 0, sizeof vis);
    	memset(cnt, 0, sizeof cnt);
    	fill(dis, dis+n+1, -inf);
    	dis[0] = 0; vis[0] = 1;
    	q.push(0);
    	while(!q.empty())
    	{
    		int u = q.front(); q.pop();
    		vis[u] = 0; ++cnt[u];
    		if(cnt[u] > n) return ;
    		for(int k = head[u]; k != -1; k = edge[k].next)
    		{
    			int v = edge[k].v;
    			if(dis[v] < dis[u]+edge[k].w)
    			{
    				dis[v] = dis[u]+edge[k].w;
    				if(!vis[v]) q.push(v), vis[v] = 1;
    			}
    		}
    	}
    }
    int main()
    {
    	int u, v, w, ans;
    	while(~scanf("%d %d", &n, &m))
    	{
    		init(); ans = 0;
    		for(int i = 1; i <= m; ++i)
    		{
    			scanf("%d %d %d", &u, &v, &w);
    			add(u+1, v+1, w);
    		}
    		for(int i = 1; i <= n; ++i) add(0, i, 0);
    		SPFA(); 
    		for(int i = 1; i <= n; ++i) ans = max(ans, dis[i]);
    		printf("%d
    ", ans+1);
    	}
    	return 0;
    }
  • 相关阅读:
    十个能让你成为牛逼前端程序猿的特征
    一道Javascript面试题引发的血案
    程序员实现财务自由的9个阶段,你达到了哪一段?
    程序员进阶路上不能错过的史上最全技术知识图谱秘籍
    清华大学研发神技能:用意念回复微信
    机器学习原来如此有趣:用深度学习识别人脸
    【代码片段】如何使用CSS来快速定义多彩光标
    Android自定义一款带进度条的精美按键
    现在的人工智能逆天到什么地步了?
    分享几套生成iMac相关高逼格免费mockup的素材和在线工具
  • 原文地址:https://www.cnblogs.com/csushl/p/9386772.html
Copyright © 2020-2023  润新知