zoukankan      html  css  js  c++  java
  • poj_1511Invitation Cards

    Invitation Cards
    Time Limit: 8000MS   Memory Limit: 262144K
    Total Submissions: 16025   Accepted: 5203

    Description

    In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 

    The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

    All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 

    Input

    The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

    Output

    For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

    Sample Input

    2
    2 2
    1 2 13
    2 1 33
    4 6
    1 2 10
    2 1 60
    1 3 20
    3 4 10
    2 4 5
    4 1 50

    Sample Output

    46
    210

    Dijkstra + 邻接表 + 堆优化

    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1000005;
    const __int64 INF = 0x7FFFFFFF;
    
    bool vis[MAXN];
    int n, m, first[MAXN], opp_first[MAXN], que[MAXN];//opp_first[i]是反向边的
    __int64 dist[MAXN], ans;
    
    typedef struct ENode
    {
    	int v,w;
    	int next;
    }ENode;
    
    ENode edge[MAXN], opp_edge[MAXN];//opp_edge[]是反向边
    
    struct node
    {
    	int u;
    	__int64 dis;
    	bool operator < (const node &a) const
    	{
    		return dis > a.dis;
    	}
    };
    
    void dijkstra(int *first,ENode *edge)
    {
    	int i, u, v;
    	for(i = 1; i <= n; i++)
    	{
    		vis[i] = 0;
    		dist[i] = INF;  //初始最短路程
    	}
    	dist[1] = 0; 
    	priority_queue<node> que; //node类型的优先队列
    	node a;
    	a.u = 1;
    	a.dis = 0;
    	que.push(a);
    	while(!que.empty())
    	{
    		u = que.top().u;
    		que.pop();
    		if(vis[u])//除去同时进几次的
    		{
    			continue;
    		}
    		vis[u] = true;
    		for(i = first[u]; i != 0; i = edge[i].next)
    		{
    			v = edge[i].v;
    			if(!vis[v] && dist[v] - edge[i].w > dist[u])
    			{
    				dist[v] = dist[u] + edge[i].w;
    				a.u = v;
    				a.dis = dist[v];
    				que.push(a);//a可能会进几次
    			}
    		}
    	}
    
    	for(i = 1; i <= n; i++)
    	{
    		ans += dist[i];
    	}
    }
    
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, u, v, w, i;
    	scanf("%d", &t);
    	while(t--)
    	{
    		scanf("%d %d", &n, &m);
    		memset(first, 0, sizeof(first));
    		memset(opp_first, 0, sizeof(opp_first));
    		for(i = 1; i <= m; i++)
    		{
    			scanf("%d %d %d", &u, &v, &w);
    			edge[i].v = v;
    			edge[i].w = w;
    			edge[i].next = first[u];
    			first[u] = i;
    
    			opp_edge[i].v = u;//反向边
    			opp_edge[i].w = w;
    			opp_edge[i].next = opp_first[v];
    			opp_first[v] = i;
    		}
    		ans = 0;
    		dijkstra(first, edge);//正向边
    		dijkstra(opp_first, opp_edge);//反向
    		printf("%I64d\n", ans);
    	} 
    	return 0;
    }

    静态邻接表+SPFA

    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1000005;
    const __int64 INF = 0x7FFFFFFF;
    
    typedef struct Node
    {
    	int v;
    	int value;
    	int next;
    }Node;
    
    Node edge[MAXN], opp_edge[MAXN];//opp_edge[]是反向边
    bool vis[MAXN];
    int n, m, first[MAXN], opp_first[MAXN];//opp_first[i]是反向边的
    __int64 dist[MAXN], ans;
    queue<int> Q; //node类型的优先队列
    
    void SPFA(int Start, int *pre, Node * src)
    {
    	while (!Q.empty())
    	{
    		Q.pop();
    	}
    	for (int i = 1; i <= n; i++)
    	{
    		vis[i] = false;
    		dist[i] = INF;
    	}
    	dist[Start] = 0;
    	vis[Start] = true;
    	Q.push(Start);
    	while (!Q.empty())
    	{
    		int top = Q.front();
    		Q.pop();
    		vis[top] = false;
    		for (int i = pre[top]; i != -1; i = src[i].next)
    		{
    			int e = src[i].v;
    			if(dist[e] > dist[top] + src[i].value)
    			{
    				dist[e] = dist[top] + src[i].value;
    				if(!vis[e])
    				{
    					vis[e] = true;
    					Q.push(e);
    				}
    			}
    		}
    	}
    	for (int i = 1; i <= n; i++)
    	{
    		ans += dist[i];
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, u, v, w, i;
    	scanf("%d", &t);
    	while(t--)
    	{
    		scanf("%d %d", &n, &m);
    		memset(first, -1, sizeof(first));
    		memset(opp_first, -1, sizeof(opp_first));
    		for(i = 1; i <= m; i++)
    		{
    			scanf("%d %d %d", &u, &v, &w);
    			edge[i].v = v;
    			edge[i].value = w;
    			edge[i].next = first[u];
    			first[u] = i;
    
    			opp_edge[i].v = u;//反向边
    			opp_edge[i].value = w;
    			opp_edge[i].next = opp_first[v];
    			opp_first[v] = i;
    		}
    		ans = 0;
    		SPFA(1, first, edge);//正向边
    		SPFA(1, opp_first, opp_edge);//反向
    		printf("%I64d\n", ans);
    	} 
    	return 0;
    }


  • 相关阅读:
    ScheduledThreadPoolExecutor源码解读
    Spring事务源码阅读笔记
    Spring AOP的实现研究
    cglib之Enhancer
    Spring IOC容器创建bean过程浅析
    CompletionService简讲
    Spring Boot:关于“No converter found for return value of type: class xxx”的解决方法
    java关于Date转换成字符串格式以及Date的增加
    jsp中文乱码六种情况---解决方案
    MYSQL给表添加字段
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835034.html
Copyright © 2011-2022 走看看