348. Continental hegemony "SDOI2010"

# 348. Continental hegemony "SDOI2010"

# 348. Continental hegemony "SDOI2010"

Memory limit: 256 MiB time limit: 1000 ms standard input and output
Question Types: traditional evaluation methods: text comparison

Title Description

There in a distant world of two countries: China is located in the western end of the country and in mainland eastern end Jason Chris countries. God the people of the two countries are two contradictory beliefs: Jason national symbol of darkness and destruction of faith God has · Brotherton, while Chris national symbol of faith and eternal light of God in Springfield · Brotherton. In January 8012 fantasy calendar, Jason country was officially declared · Brotherton is the only God of their faith, while at the beginning of the persecution Chris Anglican faith · Spring Jason country of Brotherton. Fantasy calendar 8012 on March 2, Chris Anglican located in the eastern town of Jason oracle country town uprising. Fantasy calendar 8012 on March 7, Oracle town uprising was Jason national army repression brutal means. Fantasy calendar 8012 on March 8, Chris Jason States declared war on the country. By the hundreds of thousands of army corps consisting of Chris to open the border, and Jason Legion confrontation. In April 8012 fantasy calendar, Chris Army Corps Jason break into the defense of the town of Oracle, Chris Anglican surviving town liberated. Then the war into a stalemate, protracted. Fierce fighting, a time hail of bullets, smoke, times of hardship. Fantasy calendar 8012 on May 12 night, Springfield · Brotherton lowered oracle: "Trust me, earn eternal life ." Chris Legion morale greatly increased. As coach Chris Corps, you decide to use this opportunity to launch a surprise attack, beat Jason countries. Specifically, the state-owned Jason N cities, the M connected unidirectional path. Oracle town is the city a capital city of the country and Jason N. You only need to destroy the temple is located has great Brotherton · Jason nation's capital, the country's Jason faith, everything will fall apart as well as the army, wiped out. In order to minimize the consumption of one's own, you decide to use the robot blew accomplish this task. The only difficulty is that part of the city enchantment Jason countries have to protect, not destroy the enchantment will not be able to enter the city. And each city ward is maintained by the distribution in other cities some enchantment generator, if you want to enter a city, you have to destroy all the enchantment generator to maintain the enchantment of this city. Now you have an infinite number of robot blew, a city once entered, the robot can instantly blew detonated, destroying a target (enchantment generator, or Jason National Pantheon), of course, the robot itself will be destroyed together. You need to know: the destruction of Jason minimum time required for the country.

Input Format

The first line of two positive integers N , M . Next M rows, each row of three positive integers U I , V I , W I , expressed from a city U I city V I a one-way road, blew robot needs by this road W I Time . After N lines, each describing a city. The first is a positive integer L I , the number of sustain this circle city Ward generator used. After L I th 1 ~ N cities between the number indicating the position of each ward generator. If L I = 0 , it indicates that the city is not enchantment protection to ensure L . 1 = 0  .

Output Format

Only contains a positive integer, beat Jason minimum time required for the country.

Sample

SAMPLE INPUT

6 6
1 2 1
1 4 3
2 3 1
2 5 2
4 6 2
5 3 2
0
0
0
1 3
0
2 3 5

Sample Output

5

Sample Description

1922.jpg

Data range and tips

对于 20%的数据,满足N≤15M≤50
对于 50%的数据,满足N≤500M≤6,000
对于 100%的数据,满足N≤3,000M≤70,0001≤wi≤108
输入数据保证一定有解,且不会存在维持某个城市结界的结界发生器在这个城市内部。连接两个城市的道路可能不止一条, 也可能存在一个城市自己到自己的道路。

题解

其实这道题目是Dijkstra算法的基础运用~稍带一点拓扑;

这是一道带有限制条件的单源最短路,求解这道题则可以这样做:

  • 先不带限制的求出最短路径;
  • 在松弛节点的过程中加上限制再求出单源最短路径。
  • 则可以用两个数组dis1,dis2;其中dis1[i]为到达结点i的最短时间,dis2[i]则为到达所有限制i的节点的最短时间
  • 则:
    每一次松弛时:
    dis1[v]=min(dis1[v],max(dis1[pre],dis2[pre])+w[edge:pre→v]);
    检查到达节点所保护的城市并入度减一,更新dis2[v]:
    dis2[v]=max(dis2[v],max(dis1[pre],dis2[pre]));
    当节点达到松弛的要求并且入度为零时入堆即可

另附代码:

编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#23400 #348. 大陆争霸「SDOI2010」 Accepted 100 54 ms 1236 K C++ 17 / 1.9 K 20181613 2019-08-10 20:52:05
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
//快读
unsigned int read() {
    int x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x*=10,x+=ch-'0',ch=getchar();
    return x;
}
//变量声明
int N,M,L[3005];
vector<int> protect[3005];
//图
int head[3005],ver[70005],edge[70005],Next[70005],tot=0;
void add(int u,int v,int weight) {
	ver[++tot]=v;
    edge[tot]=weight;
	Next[tot]=head[u];
	head[u]=tot;
}
//dijkstra算法
int dis1[3005],dis2[3005],vis[3005];
priority_queue<pair<int,int> > q;
void dijkstra() {
    memset(dis1,0x3f,sizeof(dis1));
    memset(vis,0,sizeof(vis));
    dis1[1]=0;
    q.push(make_pair(0,1));
    while(!q.empty()) {
        int now=q.top().second;
        q.pop();
        if(vis[now]) continue;
        vis[now]=1;
        //dis1的处理
        for(int i=head[now];i;i=Next[i]) {
            int to=ver[i],w=edge[i];
            if(dis1[to]>max(dis1[now],dis2[now])+w) {
            	dis1[to]=max(dis1[now],dis2[now])+w;
            	if(!L[to]) q.push(make_pair(-max(dis1[to],dis2[to]),to));
			}
        }
        //dis2的处理
        for(int i=0;i<protect[now].size();i++) {
        	int target=protect[now][i];
        	L[target]--;
        	dis2[target]=max(max(dis1[now],dis2[now]),dis2[target]);
        	if(!L[target]) q.push(make_pair(-max(dis1[target],dis2[target]),target));
		}
    }
}
//执行
int main() {
    N=read();M=read();
    for(int i=1;i<=M;i++) {
        int u_i=read(),v_i=read(),w_i=read();
        if(u_i!=v_i) add(u_i,v_i,w_i);
    }
    for(int i=1;i<=N;i++) {
        L[i]=read();
        for(int j=1;j<=L[i];j++) {
            int num=read();
            protect[num].push_back(i);
        }
    }
    dijkstra();
    int ans=max(dis1[N],dis2[N]);
    printf("%d\n",ans);
    return 0;
}
子任务 #1
Accepted
得分: 100
测试点 #1
Accepted
得分: 100
用时: 9 ms
内存: 1088 KiB

输入文件(1.in

3000 52999
1 2 4384713
1 3 2976483
1 4 42433
1 5 9094931
1 6 1716347
1 7 843796
1 8 1972383
1 9 4170
<791164 bytes omitted>

答案文件(1.out

7542

用户输出

7542

系统信息

Exited with return code 0
测试点 #2
Accepted
得分: 100
用时: 2 ms
内存: 352 KiB

输入文件(2.in

500 1499
1 2 3557269
1 3 7872037
1 4 6280913
1 5 3511951
1 6 1922146
1 7 7330055
1 8 5717005
1 9 321
<21673 bytes omitted>

答案文件(2.out

2447629

用户输出

2447629

系统信息

Exited with return code 0
测试点 #3
Accepted
得分: 100
用时: 2 ms
内存: 228 KiB

输入文件(3.in

10 30
7 5 385
4 10 809
10 6 846
4 3 742
9 10 117
1 3 32
8 6 750
9 10 928
1 7 451
8 2 451
10 2 73
1 1
<185 bytes omitted>

答案文件(3.out

927

用户输出

927

系统信息

Exited with return code 0
测试点 #4
Accepted
得分: 100
用时: 2 ms
内存: 224 KiB

输入文件(4.in

15 51
1 2 774781
1 3 4677433
1 4 8331780
1 5 8304787
1 6 7989193
1 7 7041916
1 8 9261749
1 9 854448

<471 bytes omitted>

答案文件(4.out

1957

用户输出

1957

系统信息

Exited with return code 0
测试点 #5
Accepted
得分: 100
用时: 8 ms
内存: 1028 KiB

输入文件(5.in

3000 52999
1 2 627116
1 3 9597816
1 4 5777971
1 5 6144797
1 6 1470809
1 7 6321394
1 8 4798286
1 9 29
<791778 bytes omitted>

答案文件(5.out

7857

用户输出

7857

系统信息

Exited with return code 0
测试点 #6
Accepted
得分: 100
用时: 2 ms
内存: 356 KiB

输入文件(6.in

100 1200
35 68 42
25 70 501
63 59 479
46 6 465
62 28 282
43 96 492
92 37 828
54 3 605
22 83 293
96 1
<13149 bytes omitted>

答案文件(6.out

2920

用户输出

2920

系统信息

Exited with return code 0
测试点 #7
Accepted
得分: 100
用时: 8 ms
内存: 1036 KiB

输入文件(7.in

3000 52999
1 2 333250
1 3 2176941
1 4 7367336
1 5 4671986
1 6 9354970
1 7 9046673
1 8 9458566
1 9 39
<790903 bytes omitted>

答案文件(7.out

10763

用户输出

10763

系统信息

Exited with return code 0
测试点 #8
Accepted
得分: 100
用时: 9 ms
内存: 1236 KiB

输入文件(8.in

3000 62999
1 2 5601606
1 3 7742821
1 4 8465591
1 5 2614717
1 6 4491093
1 7 3310721
1 8 4651502
1 9 7
<900322 bytes omitted>

答案文件(8.out

4844

用户输出

4844

系统信息

Exited with return code 0
测试点 #9
Accepted
得分: 100
用时: 4 ms
Memory: 328 KiB

Input file ( 8.The )

500 5499
1 2 4911877
1 3 4513935
1 4 5205623
1 5 6299491
1 6 7447065
1 7 221209
1 8 6674449
1 9 3636
<67429 bytes omitted>

The answer file ( 9.out )

1863

User output

1863

system message

Exited with return code 0
Test Point # 10
Accepted
Score: 100
When used: 8 MS
Memory: 1024 KiB

Input file ( 10.in )

3000 47999
1 2 9517163
1 3 5071685
1 4 6900534
1 5 4372056
1 6 6849121
1 7 4046390
1 8 2648771
1 9 4
<727288 bytes omitted>

The answer file ( 10.out )

97686

User output

97686

system message

Exited with return code 0

 

Guess you like

Origin www.cnblogs.com/oi-zzy/p/gdoi-348.html