zoukankan      html  css  js  c++  java
  • 【U014】热浪(前向星存储方法)

    Time Limit: 1 second
    Memory Limit: 128 MB

    【问题描述】

     德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧而忧,后天下之乐而乐的精神,身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。 FJ已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点先一共经过T (1 <= T <= 2,500)个城镇,方便地标号為1到T。除了起点和终点外地每个城镇由两条双向道路连向至少两个其它地城镇。每条道路有一个通过费用(包括油费,过路费等等)。 给定一个地图,包含C (1 <= C <= 6,200)条直接连接2个城镇的道路。每条道路由道路的起点Rs,终点Re (1 <= Rs <= T; 1 <= Re <= T),和花费(1 <= Ci <= 1,000)组成。求从起始的城镇Ts (1 <= Ts <= T)到终点的城镇Te(1 <= Te <= T)最小的总费用。


    【输入格式】

    第一行: 4个由空格隔开的整数: T, C, Ts, Te 第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci

    【输出格式】

    一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。

    【数据规模】

    Sample Input1

    7 11 5 4
    2 4 2
    1 4 3
    7 2 2
    3 4 3
    5 7 5
    7 3 3
    6 1 1
    6 3 4
    2 4 3
    5 6 3
    7 2 1
    
    
    
    
    
    
    
    

    Sample Output1

    7

    【题解】

    这题就是最短路径问题。

    用spfa算法就能解决。

    练习了一下前向星的存储方式。

    next[new_e] = fisr[f];

    to[new_e] = ...

    cose[new_e]=..

    first[f] = new_e;

    【代码】

    #include <cstdio>
    #include <cstring>
    #define max_e 7000
    
    int t,c,ts,te,next[max_e*2],to[max_e*2],cost[max_e*2],ne = 0,dis[3000],exsit[3000],team[7000],first[3000];
    
    void input_data()
    {
    	scanf("%d%d%d%d",&t,&c,&ts,&te);
    	for (int i = 1;i <= c;i++) //输入n条边。 
    		{
    			int rs,re,ce;
    			scanf("%d%d%d",&rs,&re,&ce);//输入每条边的起点和终点和花费 
    			next[++ne] = first[rs]; //因为是双向边,所以要双向建立。 
    			to[ne] = re;
    			cost[ne] = ce;
    			first[rs] = ne;
    			next[++ne] = first[re];
    			to[ne] = rs;
    			cost[ne] = ce;
    			first[re] = ne;
    		}
    }
    
    void spfa()
    {
    	for (int i = 1;i <=t;i++) //一开始到任何地方的距离都为正无穷 
    		dis[i] = -1;
    	dis[ts] = 0; //除了起点为 0 
    	memset(exsit,false,sizeof(exsit));
    	exsit[ts] = true;
    	int head = 0,tail = 1;//把起点加入队列中 
    	team[1] = ts;
    	while (head != tail) //用了循环队列 所以判断条件是不等 
    		{
    			head++;
    			head = ( (head-1) % 6980) + 1;
    			int f = team[head];
    			exsit[f] = false; //取出头结点 
    			int temp = first[f];
    			while (temp != 0 ) //因为用的是前向星存储,所以判断已经找完所有除度的条件是没有前置边了。 
    				{
    					int t = to[temp];
    					if (dis[t]== -1 || dis[t] > dis[f] + cost[temp]) //如果可以更新解 则更新 
    						{
    							dis[t] = dis[f] + cost[temp];
    							if (!exsit[t]) //如果目标不在队列中 就加入到队列中去 并标记。 
    								{
    									exsit[t] = true;
    									tail++;
    									tail = ((tail-1) % 6980) + 1	;
    									team[tail] = t;
    								}
    						}
    					temp = next[temp];
    				}
    		}
    	
    }
    
    void output_ans()
    {
    	printf("%d
    ",dis[te]);	
    }
    
    int main()
    {
    	//freopen("F:\rush.txt","r",stdin);
    	input_data();
    	spfa();
    	output_ans();
    	return 0;	
    }


  • 相关阅读:
    oracle 导入数据时提示只有 DBA 才能导入由其他 DBA 导出的文件
    oracle 常用语句
    android udp 无法收到数据 (模拟器中)
    android DatagramSocket send 发送数据出错
    AtCoder ABC 128E Roadwork
    AtCoder ABC 128D equeue
    AtCoder ABC 127F Absolute Minima
    AtCoder ABC 127E Cell Distance
    CodeForces 1166E The LCMs Must be Large
    CodeForces 1166D Cute Sequences
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632403.html
Copyright © 2011-2022 走看看