zoj 3946 Highway Project (spfa + 最小生成树)
2016-04-24 17:17
274 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718
Highway Project
Time Limit: 2 Seconds
Memory Limit: 65536 KB
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
The Marjar Empire has N cities (including the capital), indexed from 0 to
N - 1 (the capital is 0) and there are M highways can be built. Building the
i-th highway costs Ci dollars. It takes Di minutes to travel between city
Xi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city
i (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
T indicating the number of test cases. For each test case:
The first contains two integers N, M (1 ≤ N,
M ≤ 105).
Then followed by M lines, each line contains four integers Xi,
Yi, Di, Ci (0 ≤
Xi, Yi < N, 0 < Di,
Ci < 105).
题意:给出N个点,编号(0-N-1),M条边,每两个点之间要修一条路,时间T和花费C,求在时间最短的情况下花费最小
思路:最短路求最短时间的同时更新最小花费(最小生成树)#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 100020
const LL INF = (1ll<<60)-1;
#define met(a, b) memset (a, b, sizeof(a))
int head
, vis
, cnt, n, m;
LL COST
, TIME
;
struct node
{
int u, v, next;
LL cost, Time;
}edge[N*4];
void Init ()
{
met (head, -1);
met (vis, 0);
cnt = 0;
for (int i=0; i<n; i++)
{
TIME[i] = INF;
COST[i] = INF;
}
}
void Add_Edge (int u, int v, LL cost, LL Time)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cost = cost;
edge[cnt].Time = Time;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void SPFA ()
{
queue <int> que;
TIME[0] = 0;
COST[0] = 0;
//vis[0] = 1;
que.push (0);
while (que.size())
{
int u = que.front(); que.pop();
vis[u] = 0;
for (int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].v;
LL cost = edge[i].cost;
LL Time = edge[i].Time;
if (TIME[v] > TIME[u] + Time || (TIME[v] == TIME[u] + Time && COST[v] > cost))
{
TIME[v] = TIME[u] + Time;
COST[v] = cost;
if (!vis[v])
{
vis[v] = 1;
que.push (v);
}
}
}
}
}
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
int u, v;
LL cost, Time;
scanf ("%d %d", &n, &m);
Init ();
while (m--)
{
scanf ("%d %d %lld %lld", &u, &v, &Time, &cost);
Add_Edge (u, v, cost, Time);
Add_Edge (v, u, cost, Time);
}
SPFA ();
LL ans1 = 0, ans2 = 0;
for (int i=0; i<n; i++)
{
ans1 += TIME[i];
ans2 += COST[i];
}
printf ("%lld %lld\n", ans1, ans2);
}
return 0;
}
Highway Project
Time Limit: 2 Seconds
Memory Limit: 65536 KB
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
The Marjar Empire has N cities (including the capital), indexed from 0 to
N - 1 (the capital is 0) and there are M highways can be built. Building the
i-th highway costs Ci dollars. It takes Di minutes to travel between city
Xi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city
i (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
Input
There are multiple test cases. The first line of input contains an integerT indicating the number of test cases. For each test case:
The first contains two integers N, M (1 ≤ N,
M ≤ 105).
Then followed by M lines, each line contains four integers Xi,
Yi, Di, Ci (0 ≤
Xi, Yi < N, 0 < Di,
Ci < 105).
Output
For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.Sample Input
2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 1 1 2 3 1 2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 2 1 2 3 1 2
Sample Output
4 3 4 4
题意:给出N个点,编号(0-N-1),M条边,每两个点之间要修一条路,时间T和花费C,求在时间最短的情况下花费最小
思路:最短路求最短时间的同时更新最小花费(最小生成树)#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 100020
const LL INF = (1ll<<60)-1;
#define met(a, b) memset (a, b, sizeof(a))
int head
, vis
, cnt, n, m;
LL COST
, TIME
;
struct node
{
int u, v, next;
LL cost, Time;
}edge[N*4];
void Init ()
{
met (head, -1);
met (vis, 0);
cnt = 0;
for (int i=0; i<n; i++)
{
TIME[i] = INF;
COST[i] = INF;
}
}
void Add_Edge (int u, int v, LL cost, LL Time)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cost = cost;
edge[cnt].Time = Time;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void SPFA ()
{
queue <int> que;
TIME[0] = 0;
COST[0] = 0;
//vis[0] = 1;
que.push (0);
while (que.size())
{
int u = que.front(); que.pop();
vis[u] = 0;
for (int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].v;
LL cost = edge[i].cost;
LL Time = edge[i].Time;
if (TIME[v] > TIME[u] + Time || (TIME[v] == TIME[u] + Time && COST[v] > cost))
{
TIME[v] = TIME[u] + Time;
COST[v] = cost;
if (!vis[v])
{
vis[v] = 1;
que.push (v);
}
}
}
}
}
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
int u, v;
LL cost, Time;
scanf ("%d %d", &n, &m);
Init ();
while (m--)
{
scanf ("%d %d %lld %lld", &u, &v, &Time, &cost);
Add_Edge (u, v, cost, Time);
Add_Edge (v, u, cost, Time);
}
SPFA ();
LL ans1 = 0, ans2 = 0;
for (int i=0; i<n; i++)
{
ans1 += TIME[i];
ans2 += COST[i];
}
printf ("%lld %lld\n", ans1, ans2);
}
return 0;
}
相关文章推荐
- jQuery面试题答案
- 字符串比较
- Gradle学习系列之三——读懂Gradle语法
- Gradle学习系列之二——创建Task的多种方法
- 设置tableviewView组的快速检索
- JAVA集合框架之set
- 隐藏文件的好方法
- 特征值分解、奇异值分解、PCA概念整理
- 第 20 章 项目实战--资讯内容[5,6]
- 第一冲刺阶段站立会议08
- 根据计算机名获取其IP
- 归来
- 剑指offer--用两个栈实现队列
- jQuery 学习一:选择器
- Gradle学习系列之一——Gradle快速入门
- IOS设计模式代理模式
- Android For JNI(六)——交叉编译,NDK概述以及文件结构,编写自己的第一个JNI工程
- Android For JNI(六)——交叉编译,NDK概述以及文件结构,编写自己的第一个JNI工程
- iOS开发实战:如何将非ARC的项目转换成ARC项目
- Oracle如何实现创建数据库、备份数据库及数据导出导入的一条龙操作